NAME
Module::Stubber - Provide hooks and handles to modules not in @INCSYNOPSIS
use Module::Stubber; stub 'Foo::Bar' => { new => sub { bless {}, $_[0]; }, stringify => sub { "$_[0]"; } }; autorequire( qr/^Acme::/ => undef ); # Block access to Acme:: modul +es autorequire( 1 => sub { my $mod = shift; local @INC; unshift @INC,$special_lib; eval "require $mod"; !$@};
DESCRIPTION
There are several scenarios when this module could be useful. I have a case of using a module which pulls in a whole host of dependencies, some of which are missing on some platforms I am targetting. By providing surrogate code, at the expense of the full module functionality, it is possible to eliminate the chain dependency and allow deployment onto a wider range of platforms.Also, this module can be used to hook to a CPAN or CPANPLUS load if the module has not already been installed (in the manner of Acme::Everything, but with more control).
Similarly, the modules might be installed elsewhere, such as a private LIB or PREFIX area. This could provide a way of hooking to these modules in such a way as to track whether and when any of these modules are being used, without stopping the functionality.
stub
The purpose of stub is to replace subs and method calls in a module that has not been required, with user supplied code. If the module has been successfully required, the genuine subs are left intact. Similarly if a successful require happens after the call to stub, the stub reference is overwritten.autorequire
This is similar in concept to AUTOLOAD, but works for module names. When a require or use happens for a module outside @INC, autorequire is called with a parameter of the module name and any import list specified.The first parameter to autorequire can be a regex, a code reference or a value. The regexp is matched against the module name, the code ref called passing in the module name, or the value tested; in each case for a true value. If this was true, the second parameter was called or tested, and execution continues if this is true. If the second parameter is false, the code carps with "Unable to require <module>".
Update: part of the concept in this RFC was released to CPAN as Module::Optional
--
I'm Not Just Another Perl Hacker
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: RFC: Module::Stubber
by stvn (Monsignor) on Nov 30, 2004 at 18:07 UTC | |
by rinceWind (Monsignor) on Dec 01, 2004 at 11:26 UTC | |
by simonflk (Pilgrim) on Dec 06, 2004 at 13:20 UTC | |
Re: RFC: Module::Stubber
by revdiablo (Prior) on Nov 30, 2004 at 18:12 UTC |