1 package Module::Pluggable;
5 use Module::Pluggable::Object;
8 # Bob Porter: Looks like you've been missing a lot of work lately.
9 # Peter Gibbons: I wouldn't say I've been missing it, Bob!
18 my ($pkg, $file) = caller;
19 # the default name for the method is 'plugins'
20 my $sub = $opts{'sub_name'} || 'plugins';
22 my ($package) = $opts{'package'} || $pkg;
23 $opts{filename} = $file;
24 $opts{package} = $package;
27 my $finder = Module::Pluggable::Object->new(%opts);
28 my $subroutine = sub { my $self = shift; return $finder->plugins(@_) };
32 my ($action,@paths) = @_;
34 $finder->{'search_path'} = ["${package}::Plugin"] if ($action eq 'add' and not $finder->{'search_path'} );
35 push @{$finder->{'search_path'}}, @paths if ($action eq 'add');
36 $finder->{'search_path'} = \@paths if ($action eq 'new');
37 return $finder->{'search_path'};
42 my ($self, $only) = @_;
45 $finder->{'only'} = $only;
48 return $finder->{'only'};
52 my ($self, $except) = @_;
54 if (defined $except) {
55 $finder->{'except'} = $except;
58 return $finder->{'except'};
63 no warnings qw(redefine prototype);
65 *{"$package\::$sub"} = $subroutine;
66 *{"$package\::search_path"} = $searchsub;
67 *{"$package\::only"} = $onlysub;
68 *{"$package\::except"} = $exceptsub;
78 Module::Pluggable - automatically give your module the ability to have plugins
83 Simple use Module::Pluggable -
86 use Module::Pluggable;
92 my $mc = MyClass->new();
93 # returns the names of all plugins installed under MyClass::Plugin::*
94 my @plugins = $mc->plugins();
98 Why would you want to do this? Say you have something that wants to pass an
99 object to a number of different plugins in turn. For example you may
100 want to extract meta-data from every email you get sent and do something
101 with it. Plugins make sense here because then you can keep adding new
102 meta data parsers and all the logic and docs for each one will be
103 self contained and new handlers are easy to add without changing the
104 core code. For that, you might do something like ...
106 package Email::Examiner;
110 use Module::Pluggable require => 1;
116 foreach my $plugin ($self->plugins) {
117 $plugin->examine($email);
125 .. and all the plugins will get a chance in turn to look at it.
127 This can be trivally extended so that plugins could save the email
128 somewhere and then no other plugin should try and do that.
129 Simply have it so that the C<examine> method returns C<1> if
130 it has saved the email somewhere. You might also wnat to be paranoid
131 and check to see if the plugin has an C<examine> method.
133 foreach my $plugin ($self->plugins) {
134 next unless $plugin->can('examine');
135 last if $plugin->examine($email);
139 And so on. The sky's the limit.
144 Provides a simple but, hopefully, extensible way of having 'plugins' for
145 your module. Obviously this isn't going to be the be all and end all of
146 solutions but it works for me.
148 Essentially all it does is export a method into your namespace that
149 looks through a search path for .pm files and turn those into class names.
151 Optionally it instantiates those classes for you.
153 =head1 ADVANCED USAGE
156 Alternatively, if you don't want to use 'plugins' as the method ...
159 use Module::Pluggable sub_name => 'foo';
164 my @plugins = $mc->foo();
167 Or if you want to look in another namespace
170 use Module::Pluggable search_path => ['Acme::MyClass::Plugin', 'MyClass::Extend'];
174 use Module::Pluggable search_dirs => ['mylibs/Foo'];
177 Or if you want to instantiate each plugin rather than just return the name
180 use Module::Pluggable instantiate => 'new';
184 # whatever is passed to 'plugins' will be passed
185 # to 'new' for each plugin
186 my @plugins = $mc->plugins(@options);
189 alternatively you can just require the module without instantiating it
192 use Module::Pluggable require => 1;
194 since requiring automatically searches inner packages, which may not be desirable, you can turn this off
198 use Module::Pluggable require => 1, inner => 0;
201 You can limit the plugins loaded using the except option, either as a string,
205 use Module::Pluggable except => 'MyClass::Plugin::Foo';
210 use Module::Pluggable except => ['MyClass::Plugin::Foo', 'MyClass::Plugin::Bar'];
215 use Module::Pluggable except => qr/^MyClass::Plugin::(Foo|Bar)$/;
218 and similarly for only which will only load plugins which match.
220 Remember you can use the module more than once
223 use Module::Pluggable search_path => 'MyClass::Filters' sub_name => 'filters';
224 use Module::Pluggable search_path => 'MyClass::Plugins' sub_name => 'plugins';
228 my @filters = $self->filters;
229 my @plugins = $self->plugins;
231 =head1 INNER PACKAGES
233 If you have, for example, a file B<lib/Something/Plugin/Foo.pm> that
234 contains package definitions for both C<Something::Plugin::Foo> and
235 C<Something::Plugin::Bar> then as long as you either have either
236 the B<require> or B<instantiate> option set then we'll also find
237 C<Something::Plugin::Bar>. Nifty!
241 You can pass a hash of options when importing this module.
243 The options can be ...
247 The name of the subroutine to create in your namespace.
249 By default this is 'plugins'
253 An array ref of namespaces to look in.
257 An array ref of directorys to look in before @INC.
261 Call this method on the class. In general this will probably be 'new'
262 but it can be whatever you want. Whatever arguments are passed to 'plugins'
263 will be passed to the method.
265 The default is 'undef' i.e just return the class name.
269 Just require the class, don't instantiate (overrides 'instantiate');
273 If set to 0 will B<not> search inner packages.
274 If set to 1 will override C<require>.
278 Takes a string, array ref or regex describing the names of the only plugins to
279 return. Whilst this may seem perverse ... well, it is. But it also
280 makes sense. Trust me.
284 Similar to C<only> it takes a description of plugins to exclude
285 from returning. This is slightly less perverse.
289 This is for use by extension modules which build on C<Module::Pluggable>:
290 passing a C<package> option allows you to place the plugin method in a
291 different package other than your own.
295 By default C<Module::Pluggable> only looks for I<.pm> files.
297 By supplying a new C<file_regex> then you can change this behaviour e.g
299 file_regex => qr/\.plugin$/
301 =head2 include_editor_junk
303 By default C<Module::Pluggable> ignores files that look like they were
304 left behind by editors. Currently this means files ending in F<~> (~),
305 the extensions F<.swp> or F<.swo>, or files beginning with F<.#>.
307 Setting C<include_editor_junk> changes C<Module::Pluggable> so it does
308 not ignore any files it finds.
315 The method C<search_path> is exported into you namespace as well.
316 You can call that at any time to change or replace the
319 $self->search_path( add => "New::Path" ); # add
320 $self->search_path( new => "New::Path" ); # replace
326 This does everything I need and I can't really think of any other
327 features I want to add. Famous last words of course
329 Recently tried fixed to find inner packages and to make it
330 'just work' with PAR but there are still some issues.
333 However suggestions (and patches) are welcome.
337 Simon Wistow <simon@thegestalt.org>
341 Copyright, 2006 Simon Wistow
343 Distributed under the same terms as Perl itself.
351 L<File::Spec>, L<File::Find>, L<File::Basename>, L<Class::Factory::Util>, L<Module::Pluggable::Ordered>