Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: Local for lexicals

by JavaFan (Canon)
on Aug 10, 2009 at 16:08 UTC ( [id://787367]=note: print w/replies, xml ) Need Help??


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

It's not the dynamic-scoping-like behaviour that's the problem
Yes, it is:
The real difficulty (for me, anyway) is making a change in an inner scope to a lexical variable in an outer scope in such a way that the change will ‘revert’ outside that scope.
That's the dynamic scoping you're after.

Make $x a package variable, use local, and it will work the way you want. I don't see the point of making $x a lexical if you don't want lexical behaviour.

Replies are listed 'Best First'.
Re^4: Local for lexicals
by JadeNB (Chaplain) on Aug 10, 2009 at 16:13 UTC
    I'm sorry; I know that I must be mis-using the terms, but it seems to me that what I'm looking for is not contrary to what one could expect of lexicals. Making a change in an inner scope that is ‘seen’ by an outer scope is what I mean by “dynamic scoping” (although I probably shouldn't), and that is, of course, no problem. Having changes to a variable within a scope undone at the end of that scope is just what scoping means—it seems to me that it has nothing to do with lexical vs. dynamic. Again, aside from magic that can be associated to copying,
    { my $temp = $x; $x = $new; ... $x = $temp; }
    does what I want, obviously without subverting Perl in XS-y ways.

    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”?

      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”?
      Yes.

      And package variables allow for dynamic scoping of their values. Lexical variables have lexical scoping. It's that simple. Perl gives you options. Want lexical scoping? Use lexical variables. Want dynamic scoping? Use package variables and local.

      What's the problem?

        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”?
        Yes.
        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?

        What's the problem?
        Wanna localise lexicals! Wanna wanna wanna!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-24 14:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found