Local variable

Source: Wikipedia, the free encyclopedia.

In

side-effects that can occur with global variables
.

Scope

Local variables may have a lexical or dynamic

mksh
)'s "local" declaration. Most other languages provide lexically scoped local variables.

In most languages, local variables are

side-effects
to functions outside of the block in which they are declared.

Programming languages that employ

call by name
semantics allow the parameters to act as aliases of the values passed as arguments, allowing the subroutine to modify variables outside its own scope.

Static local variables

A special type of local variable, called a static local, is available in many mainstream languages (including

statically allocated
) variable. In all of the above languages, static variables are declared as such with a special storage class keyword (e.g., static).

Static locals in global functions have the same lifetime as

function scope
(not global scope), as with automatic local variables.

This is distinct from other usages of the static keyword, which has several different meanings in various languages.

Local variables in Perl

Perl supports both dynamic and lexically-scoped local variables. The keyword local is used to define local dynamically-scoped variables, while my is used for local lexically-scoped variables. Since dynamic scoping is less common today, the Perl documentation warns that "local isn't what most people think of as “local”.".[2] Instead, the local keyword gives a temporary, 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.[3] To create lexically-scoped local variables, use the my operator instead.[4]

To understand how it works consider the following code:

$a = 1;
sub f() {
    local $a;
    $a = 2;
    g();
}
sub g() {
    print "$a\n";
}
g();
f();
g();

this will output:

1
2
1

This happens since the global variable $a is modified to a new temporary (local) meaning inside f(), but the global value is restored upon leaving the scope of f().

Using my in this case instead of local would have printed 1 three times since in that case the $a variable would be limited to the static scope of the function f() and not seen by g().
Randal L. Schwartz and Tom Phoenix argue that the operator local should have had a different name like save.[5]

Local variables in Ruby

Ruby as a language was inspired also by Perl, but in this case, the notation was made simpler: a global variable name must be preceded by a $ sign, like $variable_name, while a local variable has simply no $ sign in front of its name, like variable_name (while in perl all scalar values have a $ in front). Note that Ruby only provides built-in support for statically-scoped local variables like Perl's my, not dynamically-scoped local variables like Perl's local. There is at least one library for Ruby that provides dynamically-scoped variables. [6]

See also

References

  1. ^ "Current C standard" (PDF). (3.61 MB) (as of 2009). In particular, see section 6.2.4 “Storage durations of objects”, page 32.
  2. ^ perldoc.perl.org: local
  3. ^ perldoc.perl.org: perlsub: Temporary Values via local()
  4. ^ perldoc.perl.org: perlsub: Private Variables via my()
  5. .
  6. ^ Conrad Irwin. "LSpace: Dynamic scope for Ruby". December 2012 http://cirw.in/blog/lspace Retrieved 2013-10-16.