P is for Practical | |
PerlMonks |
Re^3: What can we assume in a BEGIN block ?by hv (Prior) |
on Oct 04, 2004 at 12:03 UTC ( [id://396198]=note: print w/replies, xml ) | Need Help?? |
The code under discussion is effectively this:
The significant information is that our $variable = value has both a compile-time and a runtime effect. When Perl parses the first line, it sees the variable declaration - the compile-time effect - and therefore knows how to interpret other references to this variable in the rest of the lexical scope (file scope in this case). It also sees the assignment, and compiles that to be executed at runtime (the runtime effect). When it parses the second line it sees the BEGIN block, so as soon as it has reached the end of the block it suspends parsing to execute the block. At this point the runtime assignment $initialized = 0 has not yet happened. It then resumes parsing the file, and once the entire file has been compiled it then executes the top-level code. This is the point at which the variable gets set to zero. Hugo
In Section
Seekers of Perl Wisdom
|
|