Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^6: Local for lexicals

by doom (Deacon)
on Aug 10, 2009 at 21:26 UTC ( #787415=note: print w/ replies, xml ) Need Help??

in reply to Re^5: Local for lexicals
in thread Local for lexicals

Is it really the case that setting aside a value to be restored at the end of scope is what is meant by “dynamic scoping”?
Well, sort of... what I would say is the key feature of "dynamic scoping" is that if you call some other sub it sees the current localized value of the variables. E.g. if you do this:
{ local $\="\t"; print_data( \@data ); }
then any print statements buried down in the print_data sub will now have tabs automatically appended to them.

With lexical scoping, if you say

my $some_variable = 1; { my $some_variable = 0; do_some_stuff(); }
then inside the block you get a new variable that happens to have the same name as $some_variable, and you see that new variable only inside of the block you're looking at... you don't have to worry about there being any far-ranging effects inside of the sub do_some_stuff.

Does that help?

Comment on Re^6: Local for lexicals
Select or Download Code
Re^7: Local for lexicals
by LanX (Canon) on Aug 10, 2009 at 22:12 UTC
    that's how the term is defined in the perldocs:

    dynamic scoping

    Dynamic scoping works over a dynamic scope, making variables visible throughout the rest of the block in which they are first used and in any subroutines that are called by the rest of the block. Dynamically scoped variables can have their values temporarily changed (and implicitly restored later) by a local operator. (Compare lexical scoping.) Used more loosely to mean how a subroutine that is in the middle of calling another subroutine "contains" that subroutine at run time.

    Cheers Rolf

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://787415]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2014-07-11 21:58 GMT
Find Nodes?
    Voting Booth?

    When choosing user names for websites, I prefer to use:

    Results (235 votes), past polls