Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-03-28 22:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found