Suppose that you have code with an END block. Suppose that your code uses a module which also has a END block that cleans up the module's external dependencies (eg database connections) so that the module is then unusable.
Which order do we want to run these END blocks? Well we want your user code because you might save something to the database, then we want the module unload second. Where do we think that these things will probably be placed? Well the module use will likely be at the top of your code, and your END block at the bottom of your code. So the END block that we want to execute first is likely to be the one that we saw second. Which means that LIFO is most likely to be the heuristic that gets it right.
Refering to Programming Perl, 2nd Ed on page 283 they say, You may have multiple END blocks in a file -- they will execute in reverse order of definition; that is last in, first out (LIFO). That is s that related BEGINs and ENDs will nest the way you'd expect, if you pair them up. Which is another variation of what I described. You can pair the initialization and cleanup code either in the file or in modules, and later code (which might expect to have the earlier initialized stuff there) will be entirely nested between the two.
Sure it seems odd. But it is what is most likely to do The Right Thing. (Which Perl always tries to do.)
UPDATE: I had said that the Camel's case was both a special case, and a generalization of what I described. That didn't make much sense - it is a variation.
In reply to Re: Execution order of END/CHECK vs BEGIN/INIT
by tilly
in thread Execution order of END/CHECK vs BEGIN/INIT
by belden
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |