in reply to Re: BEGIN vs initialization
in thread BEGIN vs initialization

I did go through the 'perlmod' to find the part about BEGIN executing as soon as parsed (before the rest of the file is parsed). Your explanation describes a set of events that answers all my questions.

Step 2.2 (execute Gbl) takes place before returning to parse more of the main.pl! Therefore the variables will hold valid definitions when main's BEGIN block used them.

I was picturing that in the absence of BEGIN and CHECK sort of diversions, that the entire file structure (including all the 'use' modules) would be assembled/parsed, then executed starting back at the top. That is a holdover from the old 'C'/cpp days.

That also explains why BEGIN blocks execute during a "perl -c main.pl" syntax checking run.

Thanks for a the concise answer to my fundamental question..

It is always better to have seen your target for yourself, rather than depend upon someone else's description.

Replies are listed 'Best First'.
Re^3: BEGIN vs initialization
by ikegami (Patriarch) on Dec 01, 2009 at 07:46 UTC

    I did go through the 'perlmod' to find the part about BEGIN executing as soon as parsed

    If you didn't realize that, then I'm curious as to why you placed parsing Glb first in both scenarios? It seems to indicate you knew use Glb; was executed as soon as it was parsed.

    If you knew that, the key point you were really missing was that require and thus use execute the module as any other script. It gets parsed and executed. That's got nothing to do with BEGIN.