Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: BEGIN vs initialization

by ikegami (Pope)
on Nov 29, 2009 at 00:00 UTC ( #809955=note: print w/replies, xml ) Need Help??


in reply to BEGIN vs initialization

Your questions don't make sense because there's no initialisation in Perl in the sense that you're picturing. Here's what you are missing:

  • "=" doesn't denote an initialiser in Perl. It's always just a normal assignment.
  • BEGIN blocks are executed as soon as they are compiled.
  • use Module; is the same as BEGIN { require Module; import Module; }.
  • Using require to load a file simply runs it as any other script (if it's not already loaded). It is both compiled and executed.

Taking a simplified version of your code

#main.pl use Gbl; BEGIN{ $Gbl::runContext = $Gbl::runSMTP; } ...
# GBL.pm package Gbl; our $runSMTP = 2 1;

Let's apply what I've said above to determine the order in which everything is executed
main.pl
  1. Compile the code.
    1. use Gbl; is compiled.
    2. use Glb; is executed. (BEGIN blocks are executed as soon as they are compiled.)
      1. require Gbl; is executed.
        Gbl.pm
        1. Compile the code.
          1. package Gbl; is compiled.
          2. our $runSMTP = 2; is compiled.
          3. 1; is compiled.
        2. Execute the code.
          1. our $runSMTP = 2; is executed. (This is where 2 is assigned to $runSMTP.)
          2. 1; is executed.
      2. import Gbl; is executed.
    3. BEGIN { ... } is compiled.
      1. $Gbl::runContext = $Gbl::runSMTP; is compiled.
    4. BEGIN { ... } is executed. (BEGIN blocks are executed as soon as they are compiled.)
      1. $Gbl::runContext = $Gbl::runSMTP; is executed. (This is where 2 is assigned to $runContext.)
    5. The rest of the program ("...") is compiled.
  2. Execute the code.
    1. The rest of the program ("...") is executed.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://809955]
help
Chatterbox?
[choroba]: Why don't they use tire chains?
[perldigious]: First snow, probably not prepared right away choroba.
[stevieb]: it's Quebec... they do weird stuff there :) In the mountains, it is *mandatory* for trucks to use tire chains. Many passenger vehicles do as well (but it's not mandatory for them)
[perldigious]: That stuff can hit fast... like what we just got where I'm at. Saw a few people who had slid OUT of the roundabout I have to drive through to get to work, not used to the ice yet, take awhile to get the hang of it again and make vehicle adjustments.
[stevieb]: man, when the roads are glare ice, I don't even bother going to work or out... unless I absolutely have to, or was already out in the first place
[choroba]: Tire chains are mandatory here in mountains, and the only unprepared each year are gritters
[stevieb]: I have a 3 day winter survival kit in my vehicle in the event I get snowed in in the mountains (which has happened before due to avalanches closing the roads (and once in the summer due to a massive forest fire that trapped us
[perldigious]: a handful of people in my work area did not make it, but I live pretty close and it's flat the whole way, so I didn't have any trouble. The roundabout is the worst thing I have to negotiate.
[thezip]: G'day all!
[perldigious]: Well, that and dodging the people who forget how ice works right away. :-)

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2016-12-06 16:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (112 votes). Check out past polls.