Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2024-04-23 13:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found