This is currently just pod for an idea I've had. Do you think it would be a useful module?
NAME
Module::Stubber - Provide hooks and handles to modules not in @INC
SYNOPSIS
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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.