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