The lexical/unintented-global problem needs a little more visualization. In the chatterbox, some are really confused about this part.
# Not using strict, for demonstration.
sub at_top {
print "Foo is: $foo\n";
}
# This is the body of the script, where a head-to-tail flow is in effe
+ct
my $foo = "I wanted this to be visible only for the body, not all of m
+y subs!";
at_top(); # "Foo is: "
at_bottom(); # "Foo is: I wanted ..."
sub at_bottom {
# We're in the same lexical scope!!
print "Foo is: $foo\n";
}
Using strict, but not getting a warning? Yep, because $foo is an "unintended global" (thank you for giving the phenomenon a name,
demerphq)
use strict;
my $foo = "blah";
my $bar = "hello, world!";
sub mistake {
print $foo; # Works, and prints blah
}
++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
-- vs lbh hfrq OFQ pnrfne ;)
- Whreq