local gives a value to a global variable.
While the localization is in effect the original value
is unavailable. When it ends the old value is restored.
Update: Value is the wrong word above. The technical
term is thingie. local attaches the name to another
thingie for a while
my creates a new variable which may incidentally
have the same name as a global variable. This is the common
type of declaration as in C or C++.
Usually you want to use my.
You can't my %hash; local %hash; but
it is possible to my %hash; local $hash{key} = 1000;
I haven't used or examined this usage though.
I don't, GASP, have perl on this machine, so this is untested, but will demonstrate the important differences
between lexical and dynamic scoping.
$var =1; { my $var = 2; print "\$main::var >$main::var<\n"; print "\$var >$var<\n"; &show_var; # my var doesn't exist outside this scope } { local $var = 3; # local var gives $main::var a temporary value print "\$main::var >$main::var<\n"; print "\$var >$var<\n"; &show_var; # local var ceases to exist when we _leave_ this scope } print "\$var >$var<\n"; sub showvar { print "showvar($var)\n"; }
In reply to Re: nuances of my, and maybe local
by rir
in thread nuances of my, and maybe local
by Helter
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |