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
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; print "\n";}
And then, this from out of left field:BEGIN {print MULTIPLIER => "\n";}
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?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re (tilly) 1: namespace craziness
by tilly (Archbishop) on Jan 30, 2001 at 07:49 UTC | |
Re: namespace craziness
by a (Friar) on Jan 30, 2001 at 09:31 UTC | |
Re: namespace craziness
by chipmunk (Parson) on Jan 30, 2001 at 21:10 UTC | |
by mkmcconn (Chaplain) on Jan 30, 2001 at 22:02 UTC | |
by chipmunk (Parson) on Jan 30, 2001 at 22:15 UTC |
Back to
Meditations