stvn has asked for the wisdom of the Perl Monks concerning the following question:
Take for instance, this simple package:
Then load it with this code (compile-time):package TestPackage; BEGIN { print STDERR "in BEGIN\n" } INIT { print STDERR "in INIT\n" } 1;
And you get this output:use TestPackage;
Now, load it with this code (run-time):in BEGIN in INIT
and you get this output.require TestPackage;
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.in BEGIN Too late to run INIT block at TestPackage.pm line ...
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: INIT blocks and runtime code loading
by broquaint (Abbot) on Apr 26, 2004 at 23:55 UTC | |
by jdhedden (Deacon) on Jun 24, 2005 at 14:10 UTC |