Automatic variable: Difference between revisions

Content deleted Content added
Libnoon (talk | contribs)
Perl: Add a link to dynamic scope
Monkbot (talk | contribs)
m Task 18 (cosmetic): eval 4 templates: del empty params (2×); hyphenate params (3×);
Line 20:
(Called ''local variables''.)
 
Similar to C and C++, but there is no <code>auto</code> or <code>register</code> keyword. However, the Java compiler will not allow the usage of a not-explicitly-initialized local variable and will give a compilation error (unlike C and C++ where the compiler will usually only give a warning). The Java standard demands that every local variable must be explicitly initialized before being used.<ref>{{cite web | url=http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.5 | title=4.12.5 Initial Values of Variables | publisher=Sun Microsystems | accessdateaccess-date=2008-10-17}}</ref> This differs from instance variables, which are implicitly initialized with default values (which are {{samp|0}} for numbers and {{samp|null}} for objects).
 
===Perl===
(Called ''lexical'', ''my'' or ''private'' variables.)
 
In Perl, local variables are declared using the <code>my</code> operator. Uninitialized scalars will have the value <code>undef</code>; uninitialized arrays or hashes will be <code>()</code>.<ref>{{cite web | url=http://perldoc.perl.org/perlsub.html#Private-Variables-via-my() | title=Private variables via my() - perlsub - perldoc.perl.org | publisher= | accessdateaccess-date=2008-10-17}}</ref>
 
Perl also has a <code>[[Local variable|local]]</code> operator that does not create automatic variables,<ref>{{cite web | url=http://perldoc.perl.org/perlsub.html#Temporary-Values-via-local%28%29 | title=Temporary values via local() - perlsub - perldoc.perl.org | publisher= | accessdateaccess-date=2011-02-25}}</ref> instead giving global (package) variables a temporary value, which is [[Scope_(computer_science)#Dynamic_scoping|dynamically scoped]] to the enclosing block. When the scope of the variable is left, the old value is restored.
 
==See also==