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.


In reply to variables with colons by SavannahLion

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.