http://qs321.pair.com?node_id=209278


in reply to Need help with a variable

Everything in the BEGIN scope is always compiled executed before everything else even if in your script BEGIN {} is at the end.

$var = "VARIABLE"; END {print "this is at the end:$var\n";} BEGIN {print "this is in the beginning:$var\n";} ##OUTPUT: $ perl script.pl this is the beginning: this is at the end:VARIABLE

Try running your script without the BEGIN.

--
hiseldl
What time is it? It's Camel Time!

Replies are listed 'Best First'.
Re: Re: Need help with a variable
by Jenda (Abbot) on Oct 30, 2002 at 23:54 UTC

    No it's NOT compiled before the code above. It's EXECUTED before the code above.

    perl starts from the top, compiles the code as it goes and whenever it encounters the end of a BEGIN{} block it executes the code within that block, whenever it sees a use statement it stops compiling the script, compiles the module (executing the BEGIN{} blocks and processing the uses), executes the code in the module (if there is any outside subroutines) and executes the module's import() function). Then it continues with the script. When it gets to the end of the file it starts executing the COMPILED code outside the BEGIN{} blocks.

    I hope I'm making sense.

    Jenda