And with:#!/usr/bin/perl # try.pl use strict; use warnings; use OurCommon; BEGIN { print "BEGIN in try.pl\n"; } CHECK { print "CHECK in try.pl\n"; } INIT { print "INIT in try.pl\n"; } END { print "END in try.pl\n"; } print "try.pl main code running\n";
Running that with a simple "perl try.pl" I get:#!/usr/bin/perl # OurCommon.pm use strict; use warnings; package OurCommon; BEGIN { print "BEGIN in OurCommon.pm\n"; } CHECK { print "CHECK in OurCommon.pm\n"; } INIT { print "INIT in OurCommon.pm\n"; } END { print "END in OurCommon.pm\n"; } print "OurCommon.pm non-sub code running\n"; sub new { print "new in OurCommon\n" }
BEGIN in OurCommon.pm OurCommon.pm non-sub code running BEGIN in try.pl CHECK in try.pl CHECK in OurCommon.pm INIT in OurCommon.pm INIT in try.pl try.pl main code running END in try.pl END in OurCommon.pmSo as we can see, the "non-sub" code in the module file is run while the "use" is processed, which is during the BEGIN phase. That also makes it clear why putting the code in an INIT block fixed it. I'm more educated now, but I really wished the docs made that more clear. :) Looking in "perldoc perlmod", I can see this info is implied, sort of if I squint real hard, but it's really not obvious to me at all while reading that.
In reply to Re^6: SIG, Modules, and Eval
by kbrannen
in thread SIG, Modules, and Eval
by kbrannen
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |