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.