Why is my $1 variable empty after successful match in Perl?

This should be a no-brainer, but clearly I'm missing something obvious. Why doesn't this work? I'm just trying to get the contents of the matched portion of a string. It should be $1 right? Obviously, this is a very simple test -- my bigger application is much more complicated, so please tell me what I'm doing wrong.

Perl script:

my $var1 = "Hello";
print "original string: $var1";
if ($var1 =~ /Hello/ ) {print "<br>found";}
print "<br>matched string: $1";


original string: Hello
matched string:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You forgot to capture the match i.e., you forgot the parens.

if ($var1 =~ /(Hello)/ ) {print "<br>found";}

Open in new window

datastarstarAuthor Commented:
Duh, that worked! Hope you don't mind a follow-up question. What I'm really trying to do is to get the full contents of a file and load it into a hash using eval. This worked fine until I turned tainting on (-T switch). Since taint doesn't like eval unless using $1, I tried to regex match the entire contents like this. But even with the ()'s like you suggested $1 seems empty (print $1 yields nothing)...

my $filecontents=<INFILE>;  # the file contains a text representation of a hash definition
close FILE;
$filecontents =~ /(.*)/ )
print $1;
my %myHash = (eval($1));
Is the entire file just one line?
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Ouch.  Given that small amount of info, it appears to me that you are using an awkward if not ugly approach to storing and retrieving your hash.  The Storable module or one of the config modules would be a better method.

Unless your file only has one line, you'll need to adjust how you're reading it into the scalar, which as written only reads in 1 line.
datastarstarAuthor Commented:
This line might be important for you to see:

undef $/;
my $filecontents=<INFILE>;

The file is actually very large -- hundreds of lines of hash defs.  If there's a better way to create a hash from a file, I'd love to hear it.

do "filename";
$filecontents =~ /(.*)/s
I would need to see a sample of your file to see if we can parse it directly with one of the config modules without altering how it gets created.

Here's an example how you can use the Storable module to store the hash in a file for later retrieval either in the same script or some other script.


use strict;
use warnings;
use Storable qw(store retrieve freeze thaw dclone);

my %orig_hash = (
    foo => 'foo',
    bar => 'bar',
    baz => 'baz'

store(\%orig_hash, 'hash.cfg');

# you can retrieve the hash data as a regular hash or as a hash_ref
my %retrieved_hash = %{ retrieve('hash.cfg') };

foreach my $key (keys %retrieved_hash) {
    print "$retrieved_hash{$key}\n";

Open in new window

If the file defines multiple hashes, then the Storable module may not best solution.  In that case, I'd go with one of the other config modules.

If the file is setup correctly, then the do "filename" as ozo suggests would be the shortest/easiest solution, but is not the approach I would personally use in a production script.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
datastarstarAuthor Commented:
Thanks to you both. I'm using Ozo's fix initially but plan to adopt FishMonger's suggestion moving forward. Since FM answered the original question, I did a 2/3 - 1/3 split of the points. Hope that's fair.

Thanks again!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.