Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Need help with a variable

by hiseldl (Priest)
on Oct 30, 2002 at 23:38 UTC ( #209278=note: print w/replies, xml ) Need Help??

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 this is the beginning: this is at the end:VARIABLE

Try running your script without the BEGIN.

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.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2022-05-22 12:21 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (80 votes). Check out past polls.