Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Re: Can't localize lexical variable $var at...

by Courage (Parson)
on Jun 09, 2002 at 19:17 UTC ( [id://172941]=note: print w/replies, xml ) Need Help??


in reply to Re: Can't localize lexical variable $var at...
in thread Can't localize lexical variable $var at...

you wrote:
In particular, a sub outside the scope cannot see that variable.

But subs inside that scope are able to see that! And I cleanly see a way how that lexically scoped variable could be stacked, just like global variable!

But yes, I'll better just use lexical variables and implement stacking if I really need.

Best wishes,
Courage, the Cowardly Dog

  • Comment on Re: Re: Can't localize lexical variable $var at...

Replies are listed 'Best First'.
Re^3: Can't localize lexical variable $var at...
by Aristotle (Chancellor) on Jun 09, 2002 at 20:29 UTC
    No, it cannot. If you { my $var; some_sub(); }, then some_sub() cannot see $var. On the other hand, if you { local $var; some_sub(); }, then some_sub() can. (And that's why you shouldn't local unless you have a clear reason for it and not before you have signed a contract that you clearly understand that perl is not liable if you botch up at that point, and that you accept the consequences.)

    Makeshifts last the longest.

      You did not understood what I meant.
      { my $var=2; sub inc {return $var++} sub dec {return $var--} } print inc; print dec;
      Those subs see that $var which is not seen outside that block, and perl doc says this is a trick to have private static variables.

      Courage, the Cowardly Dog.

        Now you are talking about closures. I do not see how that is relevant to localized globals?

        Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found