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

Re: [perl #109270] leak on accessing anon sub inside another one

Thread Previous | Thread Next
From:
Alexandr Gomoliako
Date:
January 30, 2012 08:54
Subject:
Re: [perl #109270] leak on accessing anon sub inside another one
Message ID:
CAKb9G9jpA1GuVw79e9w5p8Yzfr_Hwdhthf13xfjKmD9QjDZqWQ@mail.gmail.com
On Mon, Jan 30, 2012 at 4:37 AM, David Golden <xdaveg@gmail.com> wrote:
> Without knowing your exact use-case, I'll point out that another
> alternative on Perl 5.10+ could be to use state variables instead of
> my variables:
>
>  use 5.010;
>
>  sub leak {
>
>     state $foo = sub {  &$bar   };
>     state $bar = sub {  "$foo"  };
>
>     return;
>  }
>
> This only creates a single instance of $foo and $bar and thus doesn't
> leak --  which may not be what you're looking for if you really want
> to generate (and return) circular closures that change behavior with
> every invocation.

Thanks. I was able to solve this with something like:

    my ($foo0, $foo1, $foo, $bar);

    $foo0 = sub {  $foo = $foo1;  &$foo1  };
    $foo1 = sub {    &$bar    };
    $bar  = sub {  "$foo"     };

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