package Release; use strict; use warnings; sub new { my $class = shift; my ($sub, @args) = @_; bless sub { $sub->(@args); }, $class; } # Can't dereference code ref w/o calling it, # so just re-bless it? sub cancel { bless $_[0], 'ReleaseCancel'; } sub DESTROY { $_[0]->(); } package ReleaseCancel; our @ISA = 'Release'; sub DESTROY { 1 } package main; use strict; use warnings; for (1..3) { my $foo = Release->new(sub {my ($num, $msg) = @_; print "$num: Foo $msg\n"}, $_, "destroyed"); print "$_\n"; $foo->cancel if $_ == 2; print "Release foo\n"; } print "Done\n";