in reply to Getting around $? setting in END blocks

I read up on some of the documentation on END and I think the answer to your problem is pretty simple. According to perlmod: END {} blocks are executed in reverse order. The last END created is the first END that gets executed. It seems all you need to do is define your END {} at the very beginning of your program before the other ones get defined. That should allow you to set your $? variable to whatever you want since by the time its seen all of the modifications to it should have already happened.

use is really just a BEGIN {} block and BEGIN executes in the order its seen. You could create a module that has your final end block and use it the very first. Tell it to redefine $? to some other value. use all of your other modules then at the end of all that use another module that creates an END { } that saves the original exit value from the program. Something like this:

#!/usr/bin/perl use strict; use vars qw($myexitvalue); use MyLastEnd; use Foo; use Bar; use SomeOtherModule; use MyFirstEnd; # rest of script #MyLastEnd.pm END { $? = $myexitvalue; } #MyFirstEnd.pm END { $myexitvalue = $?; }

All of this is untested of course, but if I'm reading the docs right it *should* work. Hope that helps!

Replies are listed 'Best First'.
Re^2: Getting around $? setting in END blocks
by DrWhy (Chaplain) on Aug 26, 2004 at 14:21 UTC
    Can you say "DOH!"?

    Of course the "last" END block has to be compiled before the use statements get processed -- it's end blocks in use-d items that are giving me the headache. So you need:

    use vars qw($exitval); END {$? = $exitval} use LotsOStuff; use MoreStuff; use TheOtherGuy'sStuff; use StuffYouProbablyDontNeed; # your code here END {$exitval = $?}