Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by Aristotle (Chancellor)
on Jun 09, 2002 at 20:29 UTC ( #172959=note: print w/ replies, xml ) Need Help??


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

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.


Comment on Re^3: Can't localize lexical variable $var at...
Select or Download Code
Re: Re^3: Can't localize lexical variable $var at...
by Courage (Parson) on Jun 09, 2002 at 20:57 UTC
    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.

        Because I am sad I can not localize lexicals (declared via 'my'), and this item was at the very beginning of my question.

        Thanks to all people (including you) that helped me understanding this and related things.

        Courage, the Cowardly Dog.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2014-12-29 05:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls