Beefy Boxes and Bandwidth Generously Provided by pair Networks
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 ( #454738=note: print w/replies, xml ) Need Help??

in reply to Re^4: What is the scope of BEGIN? Or... when does it "begin?"
in thread What is the scope of BEGIN? Or... when does it "begin?"

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:

Compile: use Foo; Compile: BEGIN { require Foo; import Foo; } Run: require Foo; (starts compiling Compile: my $shell = $ENV{'SHELL'} || ""; (my $shell; gets declared) Compile: BEGIN { ... } Run: Code from above BEGIN block ($shell is declared but uninitialized here) Compile: 1; ( fully compiled, start running it) Run: my $shell = $ENV{'SHELL'} || ""; Run: 1; (require Foo; returns) Run: import Foo; ...


- tye        

Replies are listed 'Best First'.
Re^6: What is the scope of BEGIN? Or... when does it "begin?" (steps)
by argv (Pilgrim) on May 06, 2005 at 17:54 UTC
    Yes, clearer, but there are two things:

    1) There was a statement by someone before that BEGINs have no order in their precedence or execution, and I think your explanation shows that there is. Their order and precedence goes in sequence to a very well-defined list (the one you enumerated). Is that not true?

    2) you didn't address the open issue regarding when $ENV has value(s). Randall says they don't at BEGIN time, but it seems they do. What about this?

      2) Of course %ENV is filled in before any BEGIN ever gets run. The nearby reply shows that to be true (and anything else just doesn't make sense).

      1) Yes, I'm correct. But that just means that BEGIN blocks don't have any precedence over each other that impacts their order. They have to have an order, of course, because they don't run in parallel. The order they run in is the order they get compiled in (because they don't have any precedence over each other). (:

      - tye        

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://454738]
and the rats come out to play...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2017-01-20 09:23 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (174 votes). Check out past polls.