Click here to get back home

FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server 03-26-2008
Posted by PerlFAQ Server on March 26, 2008, 9:03 pm
Please log in for more thread options
This is an excerpt from the latest version perlfaq7.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

7.17: What's the difference between dynamic and lexical (static) scoping?
Between local() and my()?

"local($x)" saves away the old value of the global variable $x and
assigns a new value for the duration of the subroutine *which is visible
in other functions called from that subroutine*. This is done at
run-time, so is called dynamic scoping. local() always affects global
variables, also called package variables or dynamic variables.

"my($x)" creates a new variable that is only visible in the current
subroutine. This is done at compile-time, so it is called lexical or
static scoping. my() always affects private variables, also called
lexical variables or (improperly) static(ly scoped) variables.

For instance:

sub visible {
print "var has value $var\n";
}

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 $var only has that value within the block of the lexical()
function, and it is hidden from called subroutine.

In summary, local() doesn't make what you think of as private, local
variables. It gives a global variable a temporary value. my() is what
you're looking for if you want private variables.

See "Private Variables via my()" in perlsub and "Temporary Values via
local()" in perlsub for excruciating details.



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

Similar ThreadsPosted
FAQ 7.16 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? March 6, 2005, 12:03 am
FAQ 7.16 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? June 6, 2005, 11:03 pm
FAQ 7.16 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? October 25, 2005, 10:03 pm
FAQ 7.16 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? November 21, 2005, 11:03 pm
FAQ 7.16 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? December 27, 2005, 5:03 pm
FAQ 7.16 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? January 5, 2006, 11:03 pm
FAQ 7.16 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? January 15, 2006, 5:03 am
FAQ 7.16 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? February 4, 2006, 12:03 pm
FAQ 7.16 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? August 31, 2006, 9:03 am
FAQ 7.16 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? November 19, 2006, 9:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap