There is indeed a reason for this.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.