princepawn has asked for the wisdom of the Perl Monks concerning the following question:

based on some earlier input, our script code is structured as followed:
package main; END { ... cleanup code } sub main { ... script code } exit main();

My question is: why would I get the message END failed and what can I do to prevent this from happenening?

As usual, you don't see anything in the Perl docs where you need it --- nothing under Package Constructors and Destructors.

Replies are listed 'Best First'.
Re: END failed --- call queue aborted
by merlyn (Sage) on Aug 23, 2000 at 19:25 UTC
    I think END has to not call die, and if it does, you get END failed. Actually, the real question is, what does it say under perldiag for this error message? {grin} My copy says:
    END failed--cleanup aborted (F) An untrapped exception was raised while executing an END subroutine. The interpreter is immediately exited.
    Just like I thought. Don't die in your END blocks!

    -- Randal L. Schwartz, Perl hacker

Re: END failed --- call queue aborted
by chromatic (Archbishop) on Aug 23, 2000 at 19:30 UTC
    merlyn got it in one. When I have a very mysterious error, adding the 'use diagnostics' pragma (right after use strict) shows expanded error messages like the one he quoted.

    If you dump the output to a log file, you can peruse it at your convenience. All hail four Xterms!

Re (tilly) 1: END failed --- call queue aborted
by tilly (Archbishop) on Aug 23, 2000 at 19:45 UTC
    I may be missing something, but why call exit at all?

      I always write code as:

      #!/usr/bin/perl -w use strict; #[globals here] exit main(); #[subs here] sub main { #code here return 0; }

      In that case, the exit has two purposes. One, prevent execution of dangling code between the subroutines (which should be in BEGIN blocks if you meant it to be there). Two, give a place to set a break point when debugging. ...THREE! threefold! Also encourages placing objects into "sub main" so that they get destroyed in an orderly fashion. ...FOUR! And a fanatical devotion to write code that looks like C. No, wait! It was only threefold after all.

      (though I think I'm forgeting another reason...)

              - tye (but my friends call me "Tye")
      I may be missing something, but why call exit at all?

      exit doesn't mess up an END block, die does. I am not sure when calling exit in an END block is needed either though.

      Cheers,
      KM

        I wasn't saying that it was a problem. It just looked to me like someone jumping through hoops to make their Perl look like C.

        I was wondering if they were actually using the construct for something useful.