Right, I should have thought to try a simpler example, so thanks for the prompt. :)

In essesnce, the code in question run during "BEGIN" time (sort of in an implict BEGIN block) because that's when the "use" is run. I suppose that makes sense, but I've never really thought about that and I've certainly never read about it ... hence my surprose. I can show that 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";
And with:
#!/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" }
Running that with a simple "perl try.pl" I get:
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.pm
So 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.

Thanks to everyone who replied here!

In reply to Re^6: SIG, Modules, and Eval by kbrannen
in thread SIG, Modules, and Eval by kbrannen

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.