use strict; use Scope::Guard; my $x; my $code = do { my $g; my $s=sub { $g = Scope::Guard->new( sub { warn "destroyed"; $x ; } ) }; $s->(); }; $code->(); $code=undef; warn "end"; ---- Not a CODE reference at /tmp/pt1 line 11. destroyed at /tmp/pt1 line 7 during global destruction.