note
ikegami
<p>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:
<ul>
<li>"<c>=</c>" doesn't denote an initialiser in Perl. It's always just a normal assignment.
<li><c>BEGIN</c> blocks are executed as soon as they are compiled.
<li><c>use Module;</c> is the same as <c>BEGIN { require Module; import Module; }</c>.
<li>Using <c>require</c> to load a file simply runs it as any other script (if it's not already loaded). It is both compiled and executed.
</ul>
<p>Taking a simplified version of your code
<c>
#main.pl
use Gbl;
BEGIN{
$Gbl::runContext = $Gbl::runSMTP;
}
...
</c>
<c>
# GBL.pm
package Gbl;
our $runSMTP = 2
1;
</c>
<p>Let's apply what I've said above to determine the order in which everything is executed
<table border="1"><tr><td>main.pl</td></tr><tr><td>
<ol>
<li>Compile the code.
<ol>
<li><c>use Gbl;</c> is compiled.
<li><c>use Glb;</c> is executed. (<c>BEGIN</c> blocks are executed as soon as they are compiled.)
<ol>
<li><c>require Gbl;</c> is executed.
<table border="1"><tr><td>Gbl.pm</td></tr><tr><td>
<ol>
<li>Compile the code.
<ol>
<li><c>package Gbl;</c> is compiled.
<li><c>our $runSMTP = 2;</c> is compiled.
<li><c>1;</c> is compiled.
</ol>
<li>Execute the code.
<ol>
<li><c>our $runSMTP = 2;</c> is executed. (This is where <c>2</c> is assigned to <c>$runSMTP</c>.)
<li><c>1;</c> is executed.
</ol>
</ol>
</td></tr></table>
<li><c>import Gbl;</c> is executed.
</ol>
<li><c>BEGIN { ... }</c> is compiled.
<ol>
<li><c>$Gbl::runContext = $Gbl::runSMTP;</c> is compiled.
</ol>
<li><c>BEGIN { ... }</c> is executed. (<c>BEGIN</c> blocks are executed as soon as they are compiled.)
<ol>
<li><c>$Gbl::runContext = $Gbl::runSMTP;</c> is executed. (This is where <c>2</c> is assigned to <c>$runContext</c>.)
</ol>
<li>The rest of the program ("<c>...</c>") is compiled.
</ol>
<li>Execute the code.
<ol>
<li>The rest of the program ("<c>...</c>") is executed.
</ol>
</ol>
</td></tr></table>
809940
809940