develooper Front page | perl.perl5.porters | Postings from May 2015

Re: Premature freeing with "Non-eval closures don't need CvOUTSIDE"

Thread Previous | Thread Next
From:
Christian Jaeger
Date:
May 6, 2015 22:36
Subject:
Re: Premature freeing with "Non-eval closures don't need CvOUTSIDE"
Message ID:
CAEjYwfWcQsJYk8o06E3LZHcc6HZ7ine2Y=Kg3xMuHk_Do5mRkg@mail.gmail.com
2015-05-06 22:40 GMT+01:00 Eric Brine <ikegami@adaelis.com>:

>
>
> On Wed, May 6, 2015 at 1:31 AM, Christian Jaeger <chrjae@gmail.com> wrote:
>
>> Since making a (local) function visible to itself creates a cycle,
>> Scalar::Util's "weaken" needs to be used.
>
>
> Hardly. You can use a package variable.
>

To retain the dynamically scoped variable within the nested closure it
still needs to get a lexical binding (just like in the fixed example with
weaken). And using dynamic scope with an otherwise functional program is
asking for oversights and thus bugs. But, fair point, it will avoid the
need for weaken, and make the program look visually (even if not
semantically) clean. *Except*, somehow this doesn't actually work, getting
at the local'ized binding from within the closure returns undef. See [1].

[1]
https://github.com/pflanze/functional-perl/blob/master/t/perl/weaken-coderef-alternative-local

Or you can use __SUB__
>

Interesting, although this requires "use v5.16", so not usable everywhere
yet. It still also needs to be bound to a lexical for the inner closure,
but does make the code simpler (and probably a little faster). See [2].
Thanks for this tip.

[2]
https://github.com/pflanze/functional-perl/blob/master/t/perl/weaken-coderef-alternative-__SUB__

Ch.

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