Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to RFC: Module::Stubber by rinceWind

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-25 16:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found