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:
Eric Brine
Date:
May 6, 2015 21:40
Subject:
Re: Premature freeing with "Non-eval closures don't need CvOUTSIDE"
Message ID:
CALJW-qELsNeEAYBqPHWfuo1C9VHn03dwspY_9N+OcnP_vYcdcA@mail.gmail.com
On Wed, May 6, 2015 at 1:31 AM, Christian Jaeger <chrjae@gmail.com> wrote:

> Hi
>
> TLDR: coderefs captured by closures can't be weaken'ed anymore without
> being collected too early.
>
> I've started using a coding style that uses local self-recursive functions
> and at the same time returns closures to delay evaluation. 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.

sub recurse {
    ...
    local *helper = sub {
        ...
        helper(...);
        ...
    };

    helper(...);
}

Or you can use __SUB__

sub recurse {
    ...
    my $helper = sub {
        ...
        __SUB__->(...);
        ...
    };

    $helper->(...);
}

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