Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

variables with colons

by SavannahLion (Pilgrim)
on Jul 29, 2010 at 05:50 UTC ( [id://851848] : perlquestion . print w/replies, xml ) Need Help??

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"; }

What the heck?

$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"; }

This is a QaD utility to re-parse some pre-existing data for another batch of code. It's not really meant for long term use.

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

    Have you ever queried a modules version? Eg. print $List::Util::VERSION;

    Or perhaps modified a modules behaviour by setting a configuration variable? Eg.

    $Data::Dumper::Purity = 1

    The '::' are being taken as the package name separator token, hence the confusion. Just escape them:

    print "$typ\:\:$dat>>\n"

    And that problem will go away.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: variables with colons
by ikegami (Patriarch) on Jul 29, 2010 at 15:22 UTC

    Why does Perl think I'm trying to use a variable $typ:: when I can't ever declare $typ:: anyways?

    You can't declare $1 or $Module::foo either, so that's a bad criteria for defining what's a variable.

    %typ:: is a valid variable (the symbol table for package typ), so the name typ:: is necessarily valid.

    $ perl -le'$typ="abc"; $dat="def"; $typ::="123"; print "$typ::$dat"' 123def

    Workarounds:

    $ perl -le'$typ="abc"; $dat="def"; $typ::="123"; print "${typ}::$dat"' abc::def
    $ perl -le'$typ="abc"; $dat="def"; $typ::="123"; print "$typ\::$dat"' abc::def
    $ perl -le'$typ="abc"; $dat="def"; $typ::="123"; print $typ."::".$dat' abc::def
Re: variables with colons
by Neighbour (Friar) on Jul 29, 2010 at 09:59 UTC
    This is because perl doesn't know where your variablename ends and guesses the :: are part of it.
    Change your print "$typ::$dat>>\n"; to print "${typ}::${dat}>>\n"; and it should work.
    The {}'s are to explicitly tell perl which bit of the characters following $ are part of the variablename.

    Edit:
    Perls package system allows you to access, from your main program, global variables defined in such a package using $package::variablename.
    This is why perl tries to interpret the :: and then complains :)
      Note that only the first set of braces are needed - Perl cannot parse the $dat different from what the programmer intents.
Re: variables with colons
by Sue D. Nymme (Monk) on Jul 29, 2010 at 12:56 UTC

    Neighbour, above, gave you a good tip about using braces to disambiguate variable names.

    There’s another way, not as general-purpose, but quick and dirty for the kind of thing you're doing: use a backslash.

    print "$typ\::$dat\n";

    HTH