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