in reply to Reference to guard not released

Hello roman,

One way to get the desired behaviour is to change the argument to Scope::Guard->new() from an anonymous code reference (i.e., a closure) into a reference to a named sub:

#! perl use strict; use warnings; use Scope::Guard; my $x; my $code = do { my $g; sub { $g = Scope::Guard->new(\&handler); }; }; $code->(); undef $code; warn "end"; sub handler { warn "destroyed"; $x; }

which outputs:

destroyed at /tmp/sample.pl line 18. end at /tmp/sample.pl line 14.

as desired. I’m not sure why this is, but I suspect it has to do with the “deep magic” Perl uses to implement closures.

HTH,

Athanasius <°(((><contra mundum

Replies are listed 'Best First'.
Re^2: Reference to guard not released
by dolmen (Beadle) on Jul 18, 2012 at 12:53 UTC
    This is the kind of not-so-deep magic that you must understand if you use closures. Devel::RefCount may be useful to diagnose problems.