Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: BEGIN block and failure to initialize constants

by kennethk (Abbot)
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.

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?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2016-10-23 03:51 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (300 votes). Check out past polls.