Local variable: Difference between revisions

Content deleted Content added
Tags: references removed Visual edit
Line 16:
==Local variables in Perl==
 
[[Perl]] supports both dynamic and lexically-scoped local variables. The keyword <code>local</code> is used to define local dynamically-scoped variables, while <code>my</code> is used for local lexically-scoped variables. Since dynamic scoping is less common today, the Perl documentation warns that "<code>local</code> isn't what most people think of as “local”.".<ref>[http://perldoc.perl.org/functions/local.html perldoc.perl.org: local]</ref> Instead, the <code>local</code> keyword gives a temporary, [[scope (programming)|dynamically-scoped]] value to a global (package) variable, which lasts until the end of the enclosing block. However, the variable is visible to any function called from within the block.enclosingJHJJDPFJLASDJVCOIQJC<ref>[http://perldoc.perl.org/perlsub.html#Temporary-Values-via-local()source perldoc.lang="perl.org: perlsub: Temporary Values via <code>local()</code>]</ref> To create lexically-scoped local variables, use the <code>my</code> operator instead.<ref>[http://perldoc.perl.org/perlsub.html#Private-Variables-via-my() perldoc.perl.org: perlsub: Private Variables via <code>my()</code>]</ref">
 
To understand how it works consider the following code:
<source lang="perl">
$a = 1;
sub f() {