#! 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; } #### destroyed at /tmp/sample.pl line 18. end at /tmp/sample.pl line 14.