The general idea is that perl compiles the entire 'unit' (typically a file, or a string eval), then execute it. There are two exceptions:
  1. A BEGIN block. The block is run right after it is compiled. The block is compiled entirely before it's run.
  2. A use statement. This one is run as soon as it's compiled. One might see a use statement as a require and an import call inside a BEGIN block. The side-effect of a use statement is that the used module is compiled and executed.
So, it your case, the order is (ignoring all the other modules):
  1. Compile main up to the 'use Gbl;' line.
  2. Run 'use Gbl;'. This causes:
    1. Compile Gbl.pm.
    2. Run the code in the Gbl package.
    3. Call 'Gbl->import' (if Gbl::import is defined).
  3. Compile the BEGIN block.
  4. Execute the code in the BEGIN block.
  5. Compile the rest of main.pl.
  6. Run main.pl (except the already run use statements and BEGIN blocks).

In reply to Re: BEGIN vs initialization by JavaFan
in thread BEGIN vs initialization by Wiggins

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.