use strict; use warnings; use Scope::Guard; sub foo { my $blah = shift; my $sg = Scope::Guard->new( sub { print "This runs at the end of the scope\n"; } ); print "Doing some other stuff now...\n"; }