Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^4: What can we assume in a BEGIN block ?

by leriksen (Curate)
on Oct 04, 2004 at 12:36 UTC ( [id://396207]=note: print w/replies, xml ) Need Help??


in reply to Re^3: What can we assume in a BEGIN block ?
in thread What can we assume in a BEGIN block ?

Thank you hv, that helps a lot.

I suppose that begs the next point - say you have a whole bunch of modules with runtime initialisations, assignments and method/sub calls - is there anything documented as to the order these get done (the same order as the sequence of 'use' statements would be a first guess) ?

use brain;

  • Comment on Re^4: What can we assume in a BEGIN block ?

Replies are listed 'Best First'.
•Re^5: What can we assume in a BEGIN block ?
by merlyn (Sage) on Oct 04, 2004 at 12:52 UTC
    It's really rather obvious when you pay attention to the documented compile-time vs run-time components of things.

    For example, require is executed at run-time, first compiling the referenced code, then running it immediately after (because it is essentially a super-eval, which compiles and runs code at runtime). BEGIN-blocks move the enclosed code from runtime to compiletime. Since "use" is a combination of BEGIN and require, it causes an external file to be compiled, then executed, during the compilation of the current file.

    Also, variables are declared at compile time, subroutines are defined at compile time, but the initialization of variables doesn't happen until runtime.

    Using basic rules like this, and paying attention to the other notes (like how //o and eval work, for example), it's always possible to work out the execution order "from first principles".

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Yep, I think my main problem was understanding what 'parse' meant.

      Discussion below uses this code

      our $initialised = 0; BEGIN { _initialise(); $initialised = 1; }

      Initially (when I didnt understand the problem) I thought the bare (not in a BEGIN) our $initialised = 0; meant 'found a scalar declaration, allocate memory, create an entry in the symbol table that references that location,and put a 0 in that memory location. Now parse and run the BEGIN block, which will result in $initialised having a 1 in it'.

      Now I (think/know) it means 'found a scalar declaration, allocate memory, create an entry in the symbol table that references that location,and generate some opcodes that will result in a 0 being put in that location at runtime.Now parse and run the BEGIN block, which will result in $initialised having a 1 in it, which will get clobbered back to 0 by the opcodes at runtime'

      Thanx merlyn for taking the time to break this down for me. I hope I have it straight now.

      use brain;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://396207]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-20 04:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found