|Think about Loose Coupling|
Re^5: What is the scope of BEGIN? Or... when does it "begin?" (steps)by tye (Sage)
|on May 06, 2005 at 17:44 UTC||Need Help??|
Perl scripts are read by perl one line at a time and the code is compiled as it is read. Right after the '}' that ends a BEGIN block is read (compiled), the code inside the block is run. require causes a different file to be read. use does a require inside of a BEGIN block. After the last line of code of a Perl file is read (either the main script or a required module), the code in that file starts running from the top. Code in subroutines isn't run until the subroutine is called, of course. my $var= EXPR; declares $var when that statement is compiled but evaluates EXPR and assigns it to $var only when that statement is run.
That is all you need to know to trace the execution order of Perl code (things like END, INIT blocks, and string-eval require additional specific knowledge, of course).
Each statement has it's own specific "compile time" and zero or more specific "run times". People often talk about "run-time" or "compile-time" actions in broad-sounding terms and even sometimes make the mistake of talking about some global "run time" or global "compile time". But each statement has its own time when it gets compiled and own time(s) when it gets run. They can interleave in interesting ways because of things like BEGIN, use, require, and eval.
So just follow the order in which Perl reads the lines and the order things get done is quite clear.
For "use Foo;", the order is: