The difference between
my and
local
is just one of scope. All subroutines called from the
context of a local variable can access that variable, but
not the my variable:
sub foo {
local $a = 10;
my $b = 20;
bar();
}
sub bar {
print "A: $a\n"; # Just fine
print "B: $b\n"; # Warning from -w
}
If a piece of code needs to use
local in this
way, it's could probably be rewritten in a cleaner way. If
my works, you should definitly use it.
I won't condemn all use of
local, however. I
believe there are situations where you need to use
local for dynamically generated anonymous functions,
perhaps for use with
$SIG{XYZ}. Can anyone
confirm or deny this? I just remember writing signal code
once where I needed to use
local. (Of course,
this is before I learned about the signal handling packages
at CPAN.)
-Ted
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.