<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Module::Pluggable;

use strict;
use vars qw($VERSION $FORCE_SEARCH_ALL_PATHS);
use Module::Pluggable::Object;

use if $] &gt; 5.017, 'deprecate';

# ObQuote:
# Bob Porter: Looks like you've been missing a lot of work lately. 
# Peter Gibbons: I wouldn't say I've been missing it, Bob! 


$VERSION = '5.1';
$FORCE_SEARCH_ALL_PATHS = 0;

sub import {
    my $class        = shift;
    my %opts         = @_;

    my ($pkg, $file) = caller; 
    # the default name for the method is 'plugins'
    my $sub          = $opts{'sub_name'}  || 'plugins';
    # get our package 
    my ($package)    = $opts{'package'} || $pkg;
    $opts{filename}  = $file;
    $opts{package}   = $package;
    $opts{force_search_all_paths} = $FORCE_SEARCH_ALL_PATHS unless exists $opts{force_search_all_paths};


    my $finder       = Module::Pluggable::Object-&gt;new(%opts);
    my $subroutine   = sub { my $self = shift; return $finder-&gt;plugins(@_) };

    my $searchsub = sub {
              my $self = shift;
              my ($action,@paths) = @_;

              $finder-&gt;{'search_path'} = ["${package}::Plugin"] if ($action eq 'add'  and not   $finder-&gt;{'search_path'} );
              push @{$finder-&gt;{'search_path'}}, @paths      if ($action eq 'add');
              $finder-&gt;{'search_path'}       = \@paths      if ($action eq 'new');
              return $finder-&gt;{'search_path'};
    };


    my $onlysub = sub {
        my ($self, $only) = @_;

        if (defined $only) {
            $finder-&gt;{'only'} = $only;
        };
        
        return $finder-&gt;{'only'};
    };

    my $exceptsub = sub {
        my ($self, $except) = @_;

        if (defined $except) {
            $finder-&gt;{'except'} = $except;
        };
        
        return $finder-&gt;{'except'};
    };


    no strict 'refs';
    no warnings qw(redefine prototype);
    
    *{"$package\::$sub"}        = $subroutine;
    *{"$package\::search_path"} = $searchsub;
    *{"$package\::only"}        = $onlysub;
    *{"$package\::except"}      = $exceptsub;

}

1;

=pod

=head1 NAME

Module::Pluggable - automatically give your module the ability to have plugins

=head1 SYNOPSIS


Simple use Module::Pluggable -

    package MyClass;
    use Module::Pluggable;
    

and then later ...

    use MyClass;
    my $mc = MyClass-&gt;new();
    # returns the names of all plugins installed under MyClass::Plugin::*
    my @plugins = $mc-&gt;plugins(); 

=head1 EXAMPLE

Why would you want to do this? Say you have something that wants to pass an
object to a number of different plugins in turn. For example you may 
want to extract meta-data from every email you get sent and do something
with it. Plugins make sense here because then you can keep adding new 
meta data parsers and all the logic and docs for each one will be 
self contained and new handlers are easy to add without changing the 
core code. For that, you might do something like ...

    package Email::Examiner;

    use strict;
    use Email::Simple;
    use Module::Pluggable require =&gt; 1;

    sub handle_email {
        my $self  = shift;
        my $email = shift;

        foreach my $plugin ($self-&gt;plugins) {
            $plugin-&gt;examine($email);
        }

        return 1;
    }



.. and all the plugins will get a chance in turn to look at it.

This can be trivially extended so that plugins could save the email
somewhere and then no other plugin should try and do that. 
Simply have it so that the C&lt;examine&gt; method returns C&lt;1&gt; if 
it has saved the email somewhere. You might also want to be paranoid
and check to see if the plugin has an C&lt;examine&gt; method.

        foreach my $plugin ($self-&gt;plugins) {
            next unless $plugin-&gt;can('examine');
            last if     $plugin-&gt;examine($email);
        }


And so on. The sky's the limit.


=head1 DESCRIPTION

Provides a simple but, hopefully, extensible way of having 'plugins' for 
your module. Obviously this isn't going to be the be all and end all of
solutions but it works for me.

Essentially all it does is export a method into your namespace that 
looks through a search path for .pm files and turn those into class names. 

Optionally it instantiates those classes for you.

=head1 ADVANCED USAGE

Alternatively, if you don't want to use 'plugins' as the method ...

    package MyClass;
    use Module::Pluggable sub_name =&gt; 'foo';


and then later ...

    my @plugins = $mc-&gt;foo();


Or if you want to look in another namespace

    package MyClass;
    use Module::Pluggable search_path =&gt; ['Acme::MyClass::Plugin', 'MyClass::Extend'];

or directory 

    use Module::Pluggable search_dirs =&gt; ['mylibs/Foo'];


Or if you want to instantiate each plugin rather than just return the name

    package MyClass;
    use Module::Pluggable instantiate =&gt; 'new';

and then

    # whatever is passed to 'plugins' will be passed 
    # to 'new' for each plugin 
    my @plugins = $mc-&gt;plugins(@options); 


alternatively you can just require the module without instantiating it

    package MyClass;
    use Module::Pluggable require =&gt; 1;

since requiring automatically searches inner packages, which may not be desirable, you can turn this off


    package MyClass;
    use Module::Pluggable require =&gt; 1, inner =&gt; 0;


You can limit the plugins loaded using the except option, either as a string,
array ref or regex

    package MyClass;
    use Module::Pluggable except =&gt; 'MyClass::Plugin::Foo';

or

    package MyClass;
    use Module::Pluggable except =&gt; ['MyClass::Plugin::Foo', 'MyClass::Plugin::Bar'];

or

    package MyClass;
    use Module::Pluggable except =&gt; qr/^MyClass::Plugin::(Foo|Bar)$/;


and similarly for only which will only load plugins which match.

Remember you can use the module more than once

    package MyClass;
    use Module::Pluggable search_path =&gt; 'MyClass::Filters' sub_name =&gt; 'filters';
    use Module::Pluggable search_path =&gt; 'MyClass::Plugins' sub_name =&gt; 'plugins';

and then later ...

    my @filters = $self-&gt;filters;
    my @plugins = $self-&gt;plugins;
    
=head1 PLUGIN SEARCHING

Every time you call 'plugins' the whole search path is walked again. This allows 
for dynamically loading plugins even at run time. However this can get expensive 
and so if you don't expect to want to add new plugins at run time you could do


  package Foo;
  use strict;
  use Module::Pluggable sub_name =&gt; '_plugins';

  our @PLUGINS;
  sub plugins { @PLUGINS ||= shift-&gt;_plugins }
  1;

=head1 INNER PACKAGES

If you have, for example, a file B&lt;lib/Something/Plugin/Foo.pm&gt; that
contains package definitions for both C&lt;Something::Plugin::Foo&gt; and 
C&lt;Something::Plugin::Bar&gt; then as long as you either have either 
the B&lt;require&gt; or B&lt;instantiate&gt; option set then we'll also find 
C&lt;Something::Plugin::Bar&gt;. Nifty!

=head1 OPTIONS

You can pass a hash of options when importing this module.

The options can be ...

=head2 sub_name

The name of the subroutine to create in your namespace. 

By default this is 'plugins'

=head2 search_path

An array ref of namespaces to look in. 

=head2 search_dirs 

An array ref of directories to look in before @INC.

=head2 instantiate

Call this method on the class. In general this will probably be 'new'
but it can be whatever you want. Whatever arguments are passed to 'plugins' 
will be passed to the method.

The default is 'undef' i.e just return the class name.

=head2 require

Just require the class, don't instantiate (overrides 'instantiate');

=head2 inner

If set to 0 will B&lt;not&gt; search inner packages. 
If set to 1 will override C&lt;require&gt;.

=head2 only

Takes a string, array ref or regex describing the names of the only plugins to 
return. Whilst this may seem perverse ... well, it is. But it also 
makes sense. Trust me.

=head2 except

Similar to C&lt;only&gt; it takes a description of plugins to exclude 
from returning. This is slightly less perverse.

=head2 package

This is for use by extension modules which build on C&lt;Module::Pluggable&gt;:
passing a C&lt;package&gt; option allows you to place the plugin method in a
different package other than your own.

=head2 file_regex

By default C&lt;Module::Pluggable&gt; only looks for I&lt;.pm&gt; files.

By supplying a new C&lt;file_regex&gt; then you can change this behaviour e.g

    file_regex =&gt; qr/\.plugin$/

=head2 include_editor_junk

By default C&lt;Module::Pluggable&gt; ignores files that look like they were
left behind by editors. Currently this means files ending in F&lt;~&gt; (~),
the extensions F&lt;.swp&gt; or F&lt;.swo&gt;, or files beginning with F&lt;.#&gt;.

Setting C&lt;include_editor_junk&gt; changes C&lt;Module::Pluggable&gt; so it does
not ignore any files it finds.

=head2 follow_symlinks

Whether, when searching directories, to follow symlinks.

Defaults to 1 i.e do follow symlinks.

=head2 min_depth, max_depth

This will allow you to set what 'depth' of plugin will be allowed.

So, for example, C&lt;MyClass::Plugin::Foo&gt; will have a depth of 3 and 
C&lt;MyClass::Plugin::Foo::Bar&gt; will have a depth of 4 so to only get the former 
(i.e C&lt;MyClass::Plugin::Foo&gt;) do

        package MyClass;
        use Module::Pluggable max_depth =&gt; 3;
        
and to only get the latter (i.e C&lt;MyClass::Plugin::Foo::Bar&gt;)

        package MyClass;
        use Module::Pluggable min_depth =&gt; 4;


=head1 TRIGGERS

Various triggers can also be passed in to the options.

If any of these triggers return 0 then the plugin will not be returned.

=head2 before_require &lt;plugin&gt;

Gets passed the plugin name. 

If 0 is returned then this plugin will not be required either.

=head2 on_require_error &lt;plugin&gt; &lt;err&gt;

Gets called when there's an error on requiring the plugin.

Gets passed the plugin name and the error. 

The default on_require_error handler is to C&lt;carp&gt; the error and return 0.

=head2 on_instantiate_error &lt;plugin&gt; &lt;err&gt;

Gets called when there's an error on instantiating the plugin.

Gets passed the plugin name and the error. 

The default on_instantiate_error handler is to C&lt;carp&gt; the error and return 0.

=head2 after_require &lt;plugin&gt;

Gets passed the plugin name. 

If 0 is returned then this plugin will be required but not returned as a plugin.

=head1 METHODs

=head2 search_path

The method C&lt;search_path&gt; is exported into you namespace as well. 
You can call that at any time to change or replace the 
search_path.

    $self-&gt;search_path( add =&gt; "New::Path" ); # add
    $self-&gt;search_path( new =&gt; "New::Path" ); # replace

=head1 BEHAVIOUR UNDER TEST ENVIRONMENT

In order to make testing reliable we exclude anything not from blib if blib.pm is 
in %INC. 

However if the module being tested used another module that itself used C&lt;Module::Pluggable&gt; 
then the second module would fail. This was fixed by checking to see if the caller 
had (^|/)blib/ in their filename.

There's an argument that this is the wrong behaviour and that modules should explicitly
trigger this behaviour but that particular code has been around for 7 years now and I'm 
reluctant to change the default behaviour.

You can now (as of version 4.1) force Module::Pluggable to look outside blib in a test environment by doing either

        require Module::Pluggable;
        $Module::Pluggable::FORCE_SEARCH_ALL_PATHS = 1;
        import Module::Pluggable;

or

        use Module::Pluggable force_search_all_paths =&gt; 1;

=head1 @INC hooks and App::FatPacker

If a module's @INC has a hook and that hook is an object which has a C&lt;files()&gt; method then we will
try and require those files too. See C&lt;t/26inc_hook.t&gt; for an example.

This has allowed L&lt;App::FatPacker&gt; (as of version 0.10.0) to provide support for Module::Pluggable.

This should also, theoretically, allow someone to modify PAR to do the same thing.

=head1 FUTURE PLANS

This does everything I need and I can't really think of any other 
features I want to add. Famous last words of course (not least 
because we're up to version 5.0 at the time of writing).

However suggestions (and patches) are always welcome.

=head1 DEVELOPMENT

The master repo for this module is at

https://github.com/simonwistow/Module-Pluggable

=head1 AUTHOR

Simon Wistow &lt;simon@thegestalt.org&gt;

=head1 COPYING

Copyright, 2006 Simon Wistow

Distributed under the same terms as Perl itself.

=head1 BUGS

None known.

=head1 SEE ALSO

L&lt;File::Spec&gt;, L&lt;File::Find&gt;, L&lt;File::Basename&gt;, L&lt;Class::Factory::Util&gt;, L&lt;Module::Pluggable::Ordered&gt;

=cut 


</pre></body></html>