develooper Front page | perl.perl5.porters | Postings from January 2018

Re: Captured lexicals stolen from outer scope's pad

Thread Previous | Thread Next
From:
Paul "LeoNerd" Evans
Date:
January 23, 2018 02:35
Subject:
Re: Captured lexicals stolen from outer scope's pad
Message ID:
20180123023539.7681ccfd@shy.leonerd.org.uk
On 22 Jan 2018 18:00:43 -0000
Father Chrysostomos <sprout@cpan.org> wrote:

> 3. When the file-CV reaches run time, the 'my' declaration has its
>    run-time effect, which is to schedule the lexical (via SAVEFREESV)
>    to be cleared on scope exit.
> 4. When the file-CV finishes running, the code in scope.c that clears
>    the entry sees that it is referenced elsewhere (its refcnt is
>    high), so it swaps it out and places another one in its place.

Thank you for that bit. The first two steps (1 and 2) I knew, but these
are new to me. I haven't spent a lot of time looking into scope exits
before, so that hadn't occurred to me.

> When you have named subs closing over lexical variables, since the
> named subs only get one chance to capture the variable, you are likely
> to get the 'will not stay shared' and 'is not available' messages.
> However, knowing that the outer CV in this case is only run once,
> Perl makes an exception and suppresses the warnings.  By cloning a
> named sub, you have created a situation the code was not written ot
> account for.

Yes. I think I'm coming to the conclusion that really, `cv_clone()` is
doing lots of things quite specific to its job of cloning the protosub
into a real runnable CV as part of the runtime part of an anoymous
`sub { ... }` expression, whereas what I need to do is far more
literally (and dumbly) duplicate a running (anon or named) CV to exactly
capture all its state. I suspect that rather than my ongoing efforts to
work around the ways `cv_clone()` doesn't do what I want I might be
better off just writing myself a

  static CV *cv_dup(pTHX_ CV *cv);

instead.

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About