Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: BEGIN block and failure to initialize constants

by kennethk (Abbot)
on Sep 17, 2012 at 21:52 UTC ( [id://994122]=note: print w/replies, xml ) Need Help??


in reply to BEGIN block and failure to initialize constants

So, as described in use, use mylib; is exactly equivalent to BEGIN { require Module; Module->import( LIST ); }. When you don't define your value in a BEGIN block, the implicit BEGIN in use constant beats it in the foot race, since (as documented in BEGIN, UNITCHECK, CHECK, INIT and END)
A BEGIN code block is executed as soon as possible, that is, the moment it is completely defined, even before the rest of the containing file (or string) is parsed. You may have multiple BEGIN blocks within a file (or eval'ed string); they will execute in order of definition.
You can even have nested BEGIN blocks, where order of operations gets applied recursively. In fact, since you will use mylib; to get to your module, that's exactly what happens.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: BEGIN block and failure to initialize constants
by temporal (Pilgrim) on Sep 17, 2012 at 22:03 UTC

    Oh man, I was overthinking this problem way too much. For some reason I thought constants were a special case and forgot entirely about use's equivalence to a BEGIN block as you just described.

    Thanks for dusting that knowledge off for me kennethk.

    Also, cool note about nesting those blocks, TIL. =)

    Strange things are afoot at the Circle-K.

Log In?
Username:
Password:

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

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

    No recent polls found