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

Re(5): Toggling between two values

by chip (Curate)
on May 05, 2003 at 18:02 UTC ( #255691=note: print w/ replies, xml ) Need Help??


in reply to •Re: Re: •Re: Re: Toggling between two values
in thread Toggling between two values

No, that's not the reason for "not stay shared". The reason is that the cloning procedure has to happen at a specific runtime moment when the lexical var(s) to be cloned are alive; however, an inner named sub inside an outer named sub has no specific runtime moment in which it can grab the var and clone it. Consider the situation if the outer sub is never called at all, but the inner sub is called by the main code: When could cloning occur? There's no opportunity. On the other hand, an anonymous sub is an expression which must be evaluated, and that moment of evaluation is the moment of cloning (if cloning is required).

Your example should be a closure. However, if you find that it isn't, it's possible that the current algorithm for deciding whether a variable is at file scope (and therefore not in need of cloning) may be incorrectly ignoring the do BLOCK and other non-sub scoping structures. So if your code doesn't make a closure when exectued at file scope, you may want to file a bug.

PS: The reason vars at file scope don't trigger closure behavior is that such vars are usually global in effect and live as long as the program does; cloning references to them is therefore of no use; it may even have caused a bug with sharing state once, though I'm not really sure, and I don't relish reviewing my old p5p mail for closure bugs....

    -- Chip Salzenberg, Free-Floating Agent of Chaos


Comment on Re(5): Toggling between two values
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (13)
As of 2014-07-22 22:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (129 votes), past polls