Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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

by Courage (Parson)
on Jun 09, 2002 at 19:17 UTC ( #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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://172941]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2018-06-23 06:34 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.