There are discoveries at every bend on the road to Perl enlightenment. One of the most distracting of these, for me, has been Perl's complex and diverse kinds of namespaces.

I came up with the following simple snippet, when I was experimenting to train myself to recognize (mostly-)namespace gotchas. Seasoned monks may not find this very surprising or interesting, but if you're new as I am, I think it's worth meditating on.

Read this closely:
Updated with comments.

#!/usr/bin/perl -w use strict; package First; # set the symbol: MULTIPLIER # and make $x equal to its value. use constant MULTIPLIER => 5*5; my $x = MULTIPLIER; package Second; # set a new symbol: if $x is defined in this context, # make this MULTIPLIER the same as the first, # otherwise "2". use constant MULTIPLIER => defined $x ? First::MULTIPLIER : 2; # easy! It's an error...it's 265...it's 50? Find out :-) print MULTIPLIER * $x, "\n";

Do you know what it will print? Do you know where the values came from?
mkmcconn

UPDATE: I just found this, where tilly shows a very similar behavior.

Update:
tilly it wrapped me in another knot, to add your

BEGIN{print MULTIPLIER; print "\n";}
And on the way there, I tried the following with amusing results (which proves tye's point made to me in CB, that the => operator is not just a cute comma:
BEGIN {print MULTIPLIER => "\n";}
And then, this from out of left field:
BEGIN {print MULTIPLIER * sub{return MULTIPLIER}, "\n";} # which really ought to be written as below to get # sane results (real obfuscation value there, # but not really a namespace issue) BEGIN {print MULTIPLIER * &{sub{return MULTIPLIER}}, "\n";}

Hopefully, I wouldn't even think of doing anything like these, in real life. How would you easily know where the whacky numbers are coming from?


In reply to namespace craziness by mkmcconn

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.