Local variable: Difference between revisions

Content deleted Content added
Scopes are really blocks, not necessarily functions
Yobot (talk | contribs)
m WP:CHECKWIKI error fixes using AWB (9543)
Line 1:
In [[computer science]], a '''local variable''' is a [[Variable (programming)|variable]] that is given ''local [[scope (programming)|scope]]''. Local variable references in the [[subroutine|function]] or [[block (programming)|block]] in which it is declared override the same variable name in the larger scope. In [[programming language]]s with only two levels of visibility, local variables are contrasted with [[global variables]]. On the other hand, many [[ALGOL]]-derived languages allow any number of nested levels of visibility, with private variables, functions, constants and types hidden within them, either by nested blocks or [[nested function]]s. Local variables are fundamental to [[procedural programming]], and more generally [[modular programming]]: variables of local scope are used to avoid issues with [[side-effect (computer science)|side-effects]] that can occur with [[global variable|global variables]]s.
 
Local variables may have a lexical or dynamic [[scope (programming)|scope]], though lexical (static) scoping is far more common. In lexical scoping (or lexical scope; also called static scoping or static scope), if a variable name's scope is a certain block, then its scope is the program text of the block definition: within that block's text, the variable name exists, and is bound to the variable's value, but outside that block's text, the variable name does not exist. By contrast, in dynamic scoping (or dynamic scope), if a variable name's scope is a certain block, then its scope is that block and all functions transitively called by that block (except when overridden again by another declaration); after the block ends, the variable name does not exist. Some languages, like [[Perl]] and [[Common Lisp]], allow the programmer to choose static or dynamic scoping when defining or redefining a variable. Examples of languages that use dynamic scoping include [[Logo (programming language)|Logo]], [[Emacs lisp]], and the shell languages [[bash]], [[dash]], and the MirBSD Korn shell (mksh)'s "local" declaration. Most other languages provide lexically scoped local variables.
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.<ref>[http://perldoc.perl.org/perlsub.html#Temporary-Values-via-local() perldoc.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: