Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

•Re: Re: •Re: Re: Toggling between two values

by merlyn (Sage)
on May 05, 2003 at 17:46 UTC ( #255685=note: print w/replies, xml ) Need Help??

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

But I thought the "not stay shared" error is because the closure stuff is triggering on a named subroutine (usually inside another one). Is that not being closed on the first invocation, and subsequent invocations creating different lex vars?

And, on a completely different note, is this not a named closure?

my $coderef = do { my $counter = 0; sub { ++$counter }; }; *named_closure = $coderef;
That looks like I can call named_closure() to me, and it's a named closure.

Perhaps that's not what

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re(5): Toggling between two values
by chip (Curate) on May 05, 2003 at 18:02 UTC
    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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://255685]
[ambrus]: I hope we didn't mess up the spam filter rules again.
[ambrus]: Our spam filter rules disallow links to certain domains, and some suspicious pharses that have appeared in previous spam advertising cheap online whatevers.
[LanX]: some servers were lagging today, so I suppose the root cause
[LanX]: ambrus no recent patches
[Petroza]: no i haven't posted anything before. It was a more or less long question with a specific issue. I did post a title and the links i added where only part of the element i was searching within the code (so no purpose other than the question itself).
[1nickt]: Petroza can you go back in your browser to the preview screen, redact the links, and try to submit again?
[LanX]: copy and paste the text into your Petroza's scratchpad please
[Petroza]: yes I'll do that. instead of the url I'll write "link"

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2017-10-17 15:24 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (233 votes). Check out past polls.