Perl Debate Time ;-)
Fred Steinberg
fred at alaska.net
Sat Mar 20 18:18:54 CST 1999
Tony> I think those who want to get rid of "local" are wrong. They are two
Tony> completely different operators, used for two different purposes.
Right; dynamic vs lexcial scoping. Completely valid concepts, i.e. this is
not a debate about Perl syntax, it's a general concept.
Tony> Variables declared with "my" are completely hidden from the outside
Tony> world, /even subroutines called within the same block/, and /even if
Tony> the subroutine is calling itself recursively./ According to "perldoc
Tony> perlsub,"
...
Tony> Hope this clears up the confusion.....
Indeed; a simple example always helps, in case anyone is still confused
(and cares):
sub printfish { print "$fish "; }
$fish = 'halibut';
printfish;
{
local $fish = 'salmon';
printfish;
}
printfish;
will print "halibut salmon halibut ".
If you change the 'local' to 'my', you get: "halibut halibut halibut ".
If you remove 'local' and 'my' altogether, you get: "halibut salmon salmon ".
I tend to think of local as saving a temporary copy of the variable from
the outer scope.
I almost never use 'local', myself. But that's no reason to get rid of it.
More information about the Anchorage-pm
mailing list