I want to use a BEGIN block to set a global context variable that other modules will use to adjust their control flow. But I remember the C++ "static initialization" pattern, which ran code before 'main' was called.

The question is
-When are initializations done?
-Are initialization code waiting to execute when the block is first entered?
-Are File globals initialized at parse or execute time?
-Is there any difference when initializing from a constant? a variable? a variable that is initialized from a constant? a function call?

When I began this 2 years ago, I could find no good documentation on the pragma "vars" other than "it pre-allocates global variables". That really says nothing about scope, mechanics, accessibility,... But I did get 'our' to work, and I like the explicit "Gbl::" prefix as self documentation.

This is the module of global values.

#GBL.pm package Gbl; #== Context ============ our $runHTTP = 1; our $runSMTP = 2; our $runContext = $runHTTP; # HTTP ...variable assignment might be af +ter BEGIN #our $runContext = 1; # HTTP ...variable assignment might be a +fter BEGIN # ... 1

This is the main controlling file (which will 'use Gbl;"

#main.pl use feature qw( :5.10 ); use strict; use warnings; use File::Temp qw/ tempfile tempdir /; use Email::MIME; use lib "/usr/local/mySystem"; use Gbl; BEGIN{ # Set some Globals for this environment $Gbl::runContext = $Gbl::runSMTP; #$Gbl::runContext = 2; $Gbl::pi_LogPath = "/tmp/primarylog.log" ; # log $Gbl::ds_LogPath = "/tmp/datalog.log" ; } use parseHTMP; use parseSMTP; #...

If the initialization executes immediately after parse, then

  1. $runHTTP is set to 1 durnig Gbl parse upon 'use' in main.pl
  2. $runSMTP is set to 2 durnig Gbl parse upon 'use' in main.pl
  3. $runContext = $runHTTP upon parse (==1)
  4. $Gbl::runContext = $Gbl::runSMTP (==2) upon BEGIN in main.pl
If it is at execute time
  1. code to set $runHTTP to 1 is prepared durnig Gbl parse upon 'use' in main.pl
  2. code to set $runSMTP to 2 is prepared durnig Gbl parse upon 'use' in main.pl
  3. code to set $runContext = $runHTTP is prepared upon parse
  4. $Gbl::runContext = $Gbl::runSMTP (== undef) upon BEGIN in main.pl
  5. Execution finally passed through Gbl, causing initialization code to execute.
    $Gbl::runContext = $Gbl::runHTTP

The initializers had not yet run in Gbl to define the variables at the point when main.pl's BEGIN block runs and accesses them?

It is always better to have seen your target for yourself, rather than depend upon someone else's description.


In reply to BEGIN vs initialization by Wiggins

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.