Take for instance, this simple package:

package TestPackage; BEGIN { print STDERR "in BEGIN\n" } INIT { print STDERR "in INIT\n" } 1;
Then load it with this code (compile-time):
use TestPackage;
And you get this output:
in BEGIN in INIT
Now, load it with this code (run-time):
require TestPackage;
and you get this output.
in BEGIN Too late to run INIT block at TestPackage.pm line ...
Now I understand why this is so. The INIT blocks run in FIFO order right before the perl runtime begins. So obviously one cannot run an INIT block if one is already within the executing runtime.

My question to the monastary is; Is there any way around this? Is there anyway when my module is required, that I can run my INIT blocks without forcing the user to manually do something (call some other initialization routine)? Is it at all possible to determine if the runtime is underway yet, therefore detemining if we were loaded with "use" or "require"? I have looked at the $^C flag but unless perl was started with the -c switch it does me no good. Has anyone had these issues before, and if so what were your solutions?

Also, does anyone know more about the UNITCHECK block which may be in perl 5.9.1 or 5.10? I am under the impression this might solve my problem, but I only know a little about it, any more info would be appreciated.

-stvn

In reply to INIT blocks and runtime code loading by stvn

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.