Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

What is an INIT block?

by John M. Dlugosz (Monsignor)
on Jan 16, 2003 at 21:01 UTC ( [id://227498]=perlquestion: print w/replies, xml ) Need Help??

John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

I found the description in perlmod, and it says "Similar to BEGIN blocks, INIT blocks are run just before the Perl runtime begins execution, in ``first in, first out'' (FIFO) order." So, how is it different from BEGIN? The description of BEGIN makes it a point that they are evaluated as soon as possible. Implying that INIT is ... not ...? What does that mean? If it means after the file is finished but ahead of the main-line code, that's what they describe a CHECK block as doing. So, is there a better explaination of these somewhere?

Replies are listed 'Best First'.
Re: What is an INIT block?
by Elian (Parson) on Jan 16, 2003 at 21:35 UTC
    BEGIN blocks are run as soon as they're done being parsed.

    CHECK blocks are run after all the code has been parsed, but before main execution starts, so you can be sure that all the parsing is done.

    INIT blocks are run after parsing and CHECK blocks, but before mainline execution.

    The big difference between INIT and CHECK blocks comes in with bytecode compilation. If you're compiling to bytecode (lucky you) then CHECK blocks are run before the bytecode is dumped, and INIT blocks aren't run at all. When you load in bytecode to run, then any pending INIT blocks are run before the code starts in.

    update:Damn, swapped init and check. Fixed

Re: What is an INIT block?
by CountZero (Bishop) on Jan 16, 2003 at 21:35 UTC

    In Chapter 18 of the Camel Book it is said:

    1. The compilation phase
    (...) BEGIN blocks are handed off to the interpreter to be run as soon as they are parsed, which effectively runs them in FIFO order. (...) Any CHECK, INIT and END blocks are scheduled by the compiler for delayed execution. (...) At the end of this phase, the interpreter is again called up to execute any scheduled CHECK blocks in LIFO order.

    2. The code generation phase.
    CHECK blocks are installed by code generators, so this optional phase occurs when you explicitly use one of the code generators. These convert the compiled (but not yet run) program into C source code or serialized Perl byte codes.

    4. The Execution phase.
    (...) At the start of this phase, before your main program gets to run, all scheduled INIT blocks are executed in FIFO order. Then your main program is run. (...) When your main program finishes, any delayed END blocks are finally executed, this time in LIFO order.

    Such are the words which can be found in the Camel Book.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: What is an INIT block?
by abatkin (Sexton) on Jan 16, 2003 at 21:42 UTC
    The camel book goes in to a bit more detail in chapter 18, Compiling, when describing the life cycle of a perl program. I'll try to summarize here but basically you are correct.

    First, the program is compiled into a parse tree. Any BEGIN blocks encountered along the way are handed to the interpreter as soon as they are compiled in a FIFO order.

    Next, in the (optional) code generation phase, any CHECK blocks are run..."CHECK blocks are installed by code geneerators, so this optional phase occurs when you explicitly use one of the code generators...These convert the compiled (but not yet run) program into either C source code or serialized Perl bytecodes..."

    Next the parse tree is reconstructed if needed (depending on what happened above)

    Finally the execution phase comes (run phase). Just before the main program itself starts running the INIT blocks are run in FIFO order, then the main program itself runs.

    I hope I got that all right, since I don't know a whole lot about the code generation phase and parse tree reconstruction phase (and CHECK blocks), so perhaps someone else can elaborate further on that.

    If you are really interested in this, I would highly suggest the Camel book, Programming Perl 3rd Edition as it goes in to a lot more detail which is hard to describe here. It also happens to be an excellent book.
Re: What is an INIT block?
by Mr. Muskrat (Canon) on Jan 16, 2003 at 21:39 UTC
    perlcompile says:
    BEGIN{} blocks are executed while compiling your code. Any external state that is initialized in BEGIN{}, such as opening files, initiating database connections etc., do not behave properly. To work around this, Perl has an INIT{} block that corresponds to code being executed before your program begins running but after your program has finished being compiled. Execution order: BEGIN{}, (possible save of state through compiler back-end), INIT{}, program runs, END{}.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://227498]
Approved by jwest
Front-paged by boo_radley
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-26 03:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found