Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: BEGIN block and failure to initialize constants

by kennethk (Monsignor)
on Sep 17, 2012 at 21:52 UTC ( #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.


Comment on Re: BEGIN block and failure to initialize constants
Select or Download Code
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
Node Status?
node history
Node Type: note [id://994122]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2014-07-13 21:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (252 votes), past polls