If you've got a recent enough version of perl (5.7+ IIRC) then you can use some
B magic to access
END blocks e.g
use B;
sub B::exec_ENDs {
my $pkg = caller;
$_->object_2svref->()
for grep { $_->GV->STASH->NAME eq $pkg } B::end_av->ARRAY;
}
{
package foo;
sub pretend_to_leave {
B->exec_ENDs();
print "I'll go get my coat ...\n";
}
END { print "... another END block\n" }
END { print "foo: this will be followed by " }
}
foo->pretend_to_leave();
END { print "main: Right, I'm outta here\n" }
__output__
foo: this will be followed by ... another END block
I'll go get my coat ...
main: Right, I'm outta here
foo: this will be followed by ... another END block
So there I've created a subroutine (that I've snuck into
B) that will execute the
END blocks for the current package.