in reply to Re: Which phase am I in?
in thread Which phase am I in?

Or do something along the lines of this near the beginning of your code:

BEGIN { $__KLUDGE::phase = 'BEGIN'; } INIT { $__KLUDGE::phase = 'INIT'; } CHECK { $__KLUDGE::phase = 'CHECK'; } END { $__KLUDGE::phase = 'END'; }

And then check $__KLUDGE::phase as necessary.

Replies are listed 'Best First'.
Re^3: Which phase am I in?
by ikegami (Patriarch) on Nov 19, 2004 at 15:58 UTC
    The END (and the CHECK?) would have to be at the end of the code, not the beginning. Maybe they should be in an eval "" at the end of your script, because they're done Last Encountered, First Executed order.
      Um, no. Just like the BEGIN blocks do not have to be at the beginning of the code (but you might have to pay attention to scoping).

      Each END block, however, will be executed in LEFE order as you describe.

      Matt

        If the END doesn't need to be at the end of the program, why does the following give the wrong answer? I stand by what I said.

        sub foo { print($__KLUDGE::phase eq 'END' ? 'END' : 'not END', $/); } # Needs to be at the beginning of the program to be reliable. BEGIN { $__KLUDGE::phase = 'BEGIN'; } # Needs to be at the end of the program to be reliable. END { $__KLUDGE::phase = 'END'; } # ... END { foo(); } # ...