use strict; use Scope::Guard; my $x; my $code = do { my $tmp = sub { warn "destroyed"; $x ; }; my $g; sub { $g = Scope::Guard->new( $tmp ); } ; }; $code->(); undef $code; warn "end";