in reply to coderefs keep references to themselves

On #p5p Leonard came up with working example of taking a closure. My version of it is this:
sub Foo::DESTROY { print "foo::DESTROY called\n"; } { my $x = do {my $var; bless sub {$var}, 'Foo'; }; } __END__ foo::DESTROY called

The fact that a simple sub { "test" } isn't garbage collected is an optimization that's poorly hidden from the user. It basically means that perl doesn't have to recompile a sub. Since eval can't be cached anyway it doesn't make sense to perform that optimization there.

Replies are listed 'Best First'.
Re^2: coderefs keep references to themselves
by jettero (Monsignor) on Jul 15, 2008 at 12:04 UTC
    Oh, I see. Since the simple sub never references anything outside of it, meaning it'd not change and wouldn't need to be recompiled, the optimizer preserves it with refcount trickery? That actually makes perfect sense and answers my question below the response below.

    -Paul