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
From:
Paul LeoNerd Evans
Date:
December 25, 2011 05:16
Subject:
Re: [perl #106864] refcount for anonymous sub is 2 instead of 1
Message ID:
20111225131630.GD23592@cel.leo
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; } };

HTH

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk
ICQ# 4135350       |  Registered Linux# 179460
http://www.leonerd.org.uk/

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