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

I think what he means is that even though it has to be the last END block, there might be other, non-END code after it.

Replies are listed 'Best First'.
Re^7: Which phase am I in?
by ikegami (Patriarch) on Nov 19, 2004 at 17:31 UTC
    It's true that it may not need to be at the very end. The point was that it's wrong to put it at the beginning.
      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.

        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.