#!/users/contrib/bin/perl package Release; use strict; use warnings; sub new { my $class = shift; my ($sub, @args) = @_; bless \sub { $sub->(@args); }, $class; } sub cancel { ${$_[0]} = sub {}; } sub DESTROY { ${$_[0]}->(); } package main; use strict; use warnings; for (1..3) { my $foo = Release->new(sub {my ($num, $msg) = @_; print "Foo $msg\n"}, $_, "destroyed"); print "$_\n"; $foo->cancel if $_ == 2; print "Release foo\n"; } print "Done\n";