Here is basically what Apache::Registry does to your script, and how to recreate the error: my $code = <<EOT;
sub abc::def {
use Dmitri::Test;
print <<HTML;
Content-type: text/plain
HTML
use strict;
use warnings;
Dmitri::Test::say_hi;
}
EOT
eval $code;
abc::def();
Your 'INIT' in the module would be a valid label if it ended in a ':', or it would be a valid BEGIN block if it said "BEGIN". I'm not sure what it gets parsed as here or why it works in one situation but not the other.
Update: from perlsyn: The LABEL is optional, and if
present, consists of an identifier followed by a colon.
Update: I now see that INIT is a valid block and is in perlcompile and perlmod, though I still wouldn't know for sure why it behaves the way it does in the above situation.
| [reply] [d/l] [select] |
After some playing with the code, I think I figured out what is going on. As your reference, open the Camel book, 3rd ed, to page 484.
The thing with the INIT block is that it is executed _after_ compilation but _before_ the rest of the program is run. Another way to say it is that code in INIT block "runs just before the runtime." Then, if some code that uses INIT blocks is eval'ed, this means that the point in time _before_ runtime has passed, and INIT block does not execute.
Fine, that answered my first question. Another problem that within eval'ed INIT block,
sub A::B::c { ... }
works fine. After thinking about this, it begins to make sense to me. A subroutine definition is _not_ code, but a language construct. While something like *{xyz} = sub {}; is a statement, sub abc {} is not!
Please correct me if I'm wrong, but I think I figured it out.
| [reply] [d/l] |
INIT {
sub Dmitri::Test::ABC::new { ... }
}
works fine. So 'INIT' _is_ a valid label, no?
| [reply] [d/l] |
I just knew that special steps were required to support BEGIN blocks in mod_perl (or any app that embeds Perl) so I thought maybe something special would be required to support INIT as well, and it might not have been done. I've actually never seen an INIT block before. It's a relatively recent addition. | [reply] |
This is kinda OT to this thread, but one of the most creative use of INIT blocks I've seen is The BEGINning of the END. MeowChow doesn't seem to hang around these parts too much more, but he wrote some great obfu!
| [reply] [d/l] |
| [reply] |
I don't think it's related to the wrapping of code used in Apache::Registry because the module that contains the INIT block is not modified at all; only the actual CGI file is. The module with the INIT is just pulled in via a "use" from the CGI.
| [reply] |
It's been around for quite a while actually (it was in Camel 2), but it's not really useful the vast majority of the time, since it is called just prior to execution but after compilation, so that it can't affect that. Choosing a BEGIN block of an INIT block is rarely wrong - the opposite case would quite frequently be.
I guess it's one of those things that you almost never need, but are very useful in that one out of 100,000 cases where you do.
Makeshifts last the longest.
| [reply] |