Pathologically Eclectic Rubbish Lister | |
PerlMonks |
What's the difference between dynamic and lexical (static) scoping? Between local() and my()?by faq_monk (Initiate) |
on Oct 08, 1999 at 00:27 UTC ( [id://694]=perlfaq nodetype: print w/replies, xml ) | Need Help?? |
Current Perl documentation can be found at perldoc.perl.org. Here is our local, out-dated (pre-5.6) version:
local($x) saves away the old value of the global variable
my($x) creates a new variable that is only visible in the current subroutine. This is done at compile-time, so is called lexical or static scoping.
For instance:
sub visible { print "var has value $var "; }
sub dynamic { local $var = 'local'; # new temporary value for the still-global visible(); # variable called $var }
sub lexical { my $var = 'private'; # new private variable, $var visible(); # (invisible outside of sub scope) }
$var = 'global';
visible(); # prints global dynamic(); # prints local lexical(); # prints global
Notice how at no point does the value ``private'' get printed. That's because
In summary,
See Private Variables via my() and Temporary Values via local() for excruciating details.
|
|