SavannahLion has asked for the wisdom of the Perl Monks concerning the following question:
Why does Perl think I'm trying to use a variable $typ:: when I can't ever declare $typ:: anyways?
I whacked my head for a good hour trying to work this one out. Readily assume that $data holds valid data and the pattern match is functioning as expected (I know the \n is in an odd place, just bear with me). Use strict and warnings are also in place. Here is a sample:
while($data =~ m/\n\s*(\w+)\s+(.*);/g){ my $typ = $1; my $dat = $2; print "$typ::$dat>>\n"; }
I get the following error:
"my" variable $typ:: can't be in a package at core_parse_2.pl line 21, + near "my $typ:: "
So two quick changes to see what's going on:
while($data =~ m/\n\s*(\w+)\s+(.*);/g){ my $typ = $1; print "Test> $typ\n"; my $dat = $2; print "$typ::$dat>>\n"; }
while($data =~ m/\n\s*(\w+)\s+(.*);/g){ print "$1::$2>>\n"; }
$1 is receiving the expected value. The assignment is working as expected. Yeah, yeah, I know that print should technically be:
while($data =~ m/\n\s*(\w+)\s+(.*);/g){ my $typ = $1; my $dat = $2; print $typ . "::" . $dat . ">>\n"; }
After wasting too much of my life, I finally worked out that perl thinks I'm trying to use $typ:: rather than $typ. Yet perl is never going to allow me to create a variable called $typ::.
Why does Perl think I'm trying to use a variable $typ:: when I can't ever declare $typ:: anyways?
and yes, I did miss the subtle :: in the error message. I get a lot of crap error message in other languages that often actually have nothing to do with the actual error itself. eg C++. I forget Perl is a little bit more concise in the error message department.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: variables with colons
by BrowserUk (Patriarch) on Jul 29, 2010 at 06:10 UTC | |
|
Re: variables with colons
by ikegami (Patriarch) on Jul 29, 2010 at 15:22 UTC | |
|
Re: variables with colons
by Neighbour (Friar) on Jul 29, 2010 at 09:59 UTC | |
by JavaFan (Canon) on Jul 29, 2010 at 11:25 UTC | |
|
Re: variables with colons
by Sue D. Nymme (Monk) on Jul 29, 2010 at 12:56 UTC |