develooper Front page | perl.perl5.porters | Postings from December 2011

Re: [perl #106864] refcount for anonymous sub is 2 instead of 1

Thread Previous | Thread Next
Paul LeoNerd Evans
December 25, 2011 05:16
Re: [perl #106864] refcount for anonymous sub is 2 instead of 1
Message ID:
On Thu, Dec 22, 2011 at 11:59:15AM -0800, Olivier Mengué wrote:
> With the following expression:
>     my $x = sub { };
> the refcount of $x is 2 instead of 1.
> There is no such problem if the sub, instead of behind a simple sub,
> is a closure.

One reference in the RV, one in the optree of the program.

Since the anonymous function is not a closure it does not need to be
cloned with its pad (environment).

I run into similar problems in the unit tests for Test::Refcount.

To create a trivial coderef with refcount 1 you need to make it a
lexical closure.

 my $x = do { my $dummy; sub { undef $dummy; } };


Paul "LeoNerd" Evans
ICQ# 4135350       |  Registered Linux# 179460

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