use strict; use Scope::Guard; my $x; my $code = do { my $g; sub { $g = Scope::Guard->new( sub { warn "destroyed"; $x ; } ); }; }; $code->(); undef $code; warn "end"; #### end at /tmp/sample.pl line 12. destroyed at /tmp/sample.pl line 7 during global destruction. #### use strict; use Scope::Guard; my $x; my $code = do { my $g; sub { $g = Scope::Guard->new( sub { warn "destroyed"; } ); }; }; $code->(); undef $code; warn "end"; #### destroyed at /tmp/sample.pl line 7. end at /tmp/sample.pl line 12.