in reply to Re: Re: version based compilation
in thread version based compilation

Sadly, it's impossible to stop compilation of the module from a VERSION sub, because the module has already been compiled before the VERSION sub is invoked! You can check that by writing a BEGIN block which prints something -- you'll see that the BEGIN block is called before the VERSION sub.

If you think about it, it has to be done that way. The module needs to be compiled first, otherwise the import() and VERSION() subs wouldn't be defined. Even though CHECK blocks are called later, it's still too late to stop them either: when a CHECK block is compiled, it's added to an internal list of routines to call at the end of compile time, and there's no way to modify that list without writing some hairy C code.

If you have my Want module installed, you can stop the module use process dead from within the VERSION method: There's an undocumented internal routine Want::double_return which diddles perl's internal data structures so that when you return from the sub you're in, the sub that called you immediately returns as well. So if your VERSION routine contains:

use Want (); Want::double_return(); return;
then the use process will be stopped right there, and the import routine will never be called. Unfortunately that still won't stop BEGIN or CHECK blocks, and anyway it's rather too much like black magic for everyday use. Fun though :-)

Replies are listed 'Best First'.
Re: Re: Re: Re: version based compilation
by steves (Curate) on Dec 30, 2001 at 04:47 UTC

    Very good info. Your Want package does about as much as I can do here it seems. I'll probably play around with that. I've learned quite a bit more about Perl today which is the Good Thing. Good point about compilation needing to be done for the subs to be defined, etc. I knew my thinking had holes in it. Thanks.