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
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.