A BEGIN block will run because those run at compilation time and require drops Perl back into the compiler.
Likewise an END block will run because that phase hasn't yet occurred when the require is executing.
However CHECK, which runs (to quote perlmod) "after the initial Perl compile phase", and INIT blocks, which run "just before the Perl runtime begins execution", are not executed since their triggers (post-initial compile phase and prior to the runtime starting) have already passed. But when they're encountered during a BEGIN block by virtue of the code being pulled in via use those phases haven't passed and hence they will be called.
$ perl -MSpoo -e 0 Spoo BEGIN Spoo CHECK Spoo INIT Spoo END $ perl -e 'require Spoo' Spoo BEGIN Spoo END $ cat Spoo.pm package Spoo; BEGIN { print "Spoo BEGIN\n"; } END { print "Spoo END\n"; } INIT { print "Spoo INIT\n"; } CHECK { print "Spoo CHECK\n"; } 1; __END__
Think of it this way: BEGIN and friends are kind of special event handler subs called by the Perl runtime. When the given event happens (compilation phase occurs, the runtime is about to start) the runtime looks into the module in question and if it has defined a handler for that event the runtime executes the corresponding sub. It's just that some of the events don't occur for require because they're one time only and that time's passed for the current execution.
Update: Slight pronoun and wording tweak in last paragraph.
The cake is a lie.
The cake is a lie.
The cake is a lie.
In reply to Re^3: use equivalent ? I suspect not...
by Fletch
in thread use equivalent ? I suspect not...
by Bloodnok
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |