in reply to Re: Getting around $? setting in END blocks
in thread Getting around $? setting in END blocks

I think you're right (I can't remember) and I never tested it on anything else as it was more a proof of concept than a serious module. That said, it does work.

A new version should be on CPAN by the time you read this. It removes the 5.8 dependency and also fixes a small bug in the error reporting, was it you who sent me the patch on RT?

As for the original question, the best way to add an END block is by writing an END block! If you want it to be triggered before all the others, put it at the end of your program, if you want it to be triggered after all the others put it at the start, before any use or require . A combination of the two can solve your problem.

my $saved_value; END { $? = $saved_value } # this will be the final END block #### your program goes here #### using whatever modules you want END { $saved_value = $? # } # this will be the first END block
The only thing that could go wrong with this scheme is that something could pull in a module containing another END block using require or eval "use something" and that END block could fiddle with $? before you get to save it. If this is happening you could try calling my_exit instead where
sub my_exit { my $value = shift; $saved_value = $value; exit $value; }