#! 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; }