in reply to Re: use equivalent ? I suspect not...
in thread use equivalent ? I suspect not...

Try again - in the expectation that the Node Reaper might step in...

TFT Fletch - twas the very insight I was too close to see.

Having said that, does that imply that a run-time require goes thro' the phases anonymously - since as we know, eval { require ... } can be used to detect run-time problems ?

Update

Observation withdrawn in the light of further, Fletch originated insight - the key being initial - just goes to show that you only read what you want to c/w what's actually there.

A user level that continues to overstate my experience :-))

Replies are listed 'Best First'.
Re^3: use equivalent ? I suspect not...
by Fletch (Bishop) on Aug 07, 2008 at 13:16 UTC

    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.

      Slightly better demo, in my opinion:

      >perl -e"print(qq{Initial compile phase over\n}); use Spoo" Spoo BEGIN Spoo CHECK Spoo INIT Initial compile phase over Spoo END >perl -e"print(qq{Initial compile phase over\n}); require Spoo" Initial compile phase over Spoo BEGIN Spoo END >perl -e"print(qq{Initial compile phase over\n}); BEGIN { require Spoo + }" Spoo BEGIN Spoo CHECK Spoo INIT Initial compile phase over Spoo END