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!


In reply to Re: Getting around $? setting in END blocks by cfreak
in thread Getting around $? setting in END blocks by DrWhy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.