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

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 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://209278]
help
Chatterbox?
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
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (80 votes). Check out past polls.

    Notices?