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

Re: Captured lexicals stolen from outer scope's pad

Thread Previous | Thread Next
Paul "LeoNerd" Evans
January 23, 2018 02:35
Re: Captured lexicals stolen from outer scope's pad
Message ID:
On 22 Jan 2018 18:00:43 -0000
Father Chrysostomos <> 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);


Paul "LeoNerd" Evans      |  |

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About