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

If that was your point, then you're wrong.

BEGINs are in first in, first out order and ENDs are in first in, last out order explicitly so that you can write code like this and trust the transactional semantics:

BEGIN { # initialize stuff } END { # cleanup stuff } # code passes, possibly you're in a different module now... BEGIN { # initialize other stuff, # which might depend on first stuff } END { # clean up other stuff, # clean up might depend on first stuff still being here }
So if your initialization and cleanup code are related, you are supposed to put them together, wherever they may appear in your code. That way if Perl has done the initialization, it will do its best to do the necessary cleanup as well.

And yes, that might mean that you put an END block at the beginning of your code. That's not wrong.

Replies are listed 'Best First'.
Re^9: Which phase am I in?
by ikegami (Patriarch) on Nov 19, 2004 at 20:02 UTC
    So if your initialization and cleanup code are related,

    They're not, at least not in the sense you mean. Read the thread. We can't use the normal case here. We want to set $phase as early as possible after BEGINs start being processed, and we want to set $phase as early as possible after ENDs start being processed. BEGIN is FIFO. END is LIFO. We want FO for both, so we need to put BEGIN as FI as possible, and the END as LI as possible.

      Ah, well in that case we could just crawl back with caller looking for calls to BEGIN or END to figure out which one we are in. Much more reliable than trying to arrange to have the very last END block inserted (particularly when we might load something that adds some more at any point).