Module/Pluggable.pm

filter: Statementstmt 78.7% 52 / 66 Branchbran 16.6% 3 / 18 Conditioncond 36.3% 4 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 81.8% 9 / 11 Podpod n/a totaltotal 61.0% 69 / 113 CC 14 SCAR 26.4
CC
14
Cov
62%
__ANON__
30.0
__ANON__
30.0
import
5.3
CRAP 14.0
« Module/Pluggable/Object.pm ↑ Summary Devel/InnerPackage.pm »
1 package Module::Pluggable;
2  
3 42 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 42 ▶use Module::Pluggable::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 42 ▶use if $] > 5.017, 'deprecate';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ObQuote:
9 # Bob Porter: Looks like you've been missing a lot of work lately.
10 # Peter Gibbons: I wouldn't say I've been missing it, Bob!
11  
12  
13 42 our $VERSION = '6.4';
14 42 our $FORCE_SEARCH_ALL_PATHS = 0;
15  
16 53 ▶sub import {
Subroutine import: called CC=4 SCAR=16.7
17 53 my $class = shift;
18 53 my %opts = @_;
19  
20 53 my ($pkg, $file) = caller;
21 # the default name for the method is 'plugins'
22 53 ▶ my $sub = $opts{'sub_name'} || 'plugins';
Condition100%2/2
$opts{'sub_name'} || 'plugins'
l!l
548
MC/DC100%1/1
$opts{'sub_name'} || 'plugins'
$opts{'sub_name'}
Truth table100%2/2
A
A= $opts{'sub_name'}
Aresult
11
00
23 # get our package
24 53 ▶ my ($package) = $opts{'package'} || $pkg;
Condition66%2/3
$opts{'package'} || $pkg
l!l&&r!l&&!r
1520
MC/DC0%0/2
$opts{'package'} || $pkg
$opts{'package'} $pkg
Truth table66%2/3
A || B
A= $opts{'package'}B= $pkg
ABresult
1X1
011
000
25 53 $opts{filename} = $file;
26 53 $opts{package} = $package;
27 53 ▶ $opts{force_search_all_paths} = $FORCE_SEARCH_ALL_PATHS unless exists $opts{force_search_all_paths};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"force_search_all_paths"} 53 0 53
28  
29  
30 53 my $finder = Module::Pluggable::Object->new(%opts);
31 53 ▶ my $subroutine = sub { my $self = shift; return $finder->plugins(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
32  
33 0 ▶ my $search_path_sub = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
34 0 my $self = shift;
35 0 my ($action,@paths) = @_;
36  
37 0 ▶ $finder->{'search_path'} = ["${package}::Plugin"] if ($action eq 'add' and not $finder->{'search_path'} );
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "add" and not $finder->{"search_path"} 0 0 0
Condition0%0/3
$action eq "add" and not $finder->{"search_path"}
!ll&&!rl&&r
000
MC/DC0%0/2
$action eq "add" and not $finder->{"search_path"}
$action eq "add" not $finder->{"search_path"}
Truth table0%0/3
A and not(B)
A= $action eq "add"B= not $finder->{"search_path"}
ABresult
0X0
100
111
38 0 ▶ push @{$finder->{'search_path'}}, @paths if ($action eq 'add');
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "add" 0 0 0
39 0 ▶ $finder->{'search_path'} = \@paths if ($action eq 'new');
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "new" 0 0 0
40 0 return $finder->{'search_path'};
41 53 };
42  
43 0 ▶ my $search_dirs_sub = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
44 0 my $self = shift;
45 0 my ($action,@paths) = @_;
46  
47 0 ▶ $finder->{'search_dirs'} = ["${package}::Plugin"] if ($action eq 'add' and not $finder->{'search_dirs'} );
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "add" and not $finder->{"search_dirs"} 0 0 0
Condition0%0/3
$action eq "add" and not $finder->{"search_dirs"}
!ll&&!rl&&r
000
MC/DC0%0/2
$action eq "add" and not $finder->{"search_dirs"}
$action eq "add" not $finder->{"search_dirs"}
Truth table0%0/3
A and not(B)
A= $action eq "add"B= not $finder->{"search_dirs"}
ABresult
0X0
100
111
48 0 ▶ push @{$finder->{'search_dirs'}}, @paths if ($action eq 'add');
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "add" 0 0 0
49 0 ▶ $finder->{'search_dirs'} = \@paths if ($action eq 'new');
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "new" 0 0 0
50 0 return $finder->{'search_dirs'};
51 53 };
52  
53 3 ▶ my $onlysub = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
54 3 my ($self, $only) = @_;
55  
56 3 ▶ if (defined $only) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $only) 3 0 3
57 3 $finder->{'only'} = $only;
58 };
59  
60 3 return $finder->{'only'};
61 53 };
62  
63 3 ▶ my $exceptsub = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
64 3 my ($self, $except) = @_;
65  
66 3 ▶ if (defined $except) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $except) 3 0 3
67 3 $finder->{'except'} = $except;
68 };
69  
70 3 return $finder->{'except'};
71 53 };
72  
73  
74 42 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
75 42 ▶ no warnings qw(redefine prototype);
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 53 *{"$package\::$sub"} = $subroutine;
78 53 *{"$package\::search_path"} = $search_path_sub;
79 53 *{"$package\::search_dirs"} = $search_dirs_sub;
80 53 *{"$package\::only"} = $onlysub;
81 53 *{"$package\::except"} = $exceptsub;
82  
83 }
84  
85 42 1;
86  
87 =pod
88  
89 =head1 NAME
90  
91 Module::Pluggable - automatically give your module the ability to have plugins
92  
93 =head1 SYNOPSIS
94  
95  
96 Simple use Module::Pluggable -
97  
98 package MyClass;
99 use Module::Pluggable;
100  
101  
102 and then later ...
103  
104 use MyClass;
105 my $mc = MyClass->new();
106 # returns the names of all plugins installed under MyClass::Plugin::*
107 my @plugins = $mc->plugins();
108  
109 =head1 EXAMPLE
110  
111 Why would you want to do this? Say you have something that wants to pass an
112 object to a number of different plugins in turn. For example you may
113 want to extract meta-data from every email you get sent and do something
114 with it. Plugins make sense here because then you can keep adding new
115 meta data parsers and all the logic and docs for each one will be
116 self contained and new handlers are easy to add without changing the
117 core code. For that, you might do something like ...
118  
119 package Email::Examiner;
120  
121 use strict;
122 use Email::Simple;
123 use Module::Pluggable require => 1;
124  
125 sub handle_email {
126 my $self = shift;
127 my $email = shift;
128  
129 foreach my $plugin ($self->plugins) {
130 $plugin->examine($email);
131 }
132  
133 return 1;
134 }
135  
136  
137  
138 .. and all the plugins will get a chance in turn to look at it.
139  
140 This can be trivially extended so that plugins could save the email
141 somewhere and then no other plugin should try and do that.
142 Simply have it so that the C<examine> method returns C<1> if
143 it has saved the email somewhere. You might also want to be paranoid
144 and check to see if the plugin has an C<examine> method.
145  
146 foreach my $plugin ($self->plugins) {
147 next unless $plugin->can('examine');
148 last if $plugin->examine($email);
149 }
150  
151  
152 And so on. The sky's the limit.
153  
154  
155 =head1 DESCRIPTION
156  
157 Provides a simple but, hopefully, extensible way of having 'plugins' for
158 your module. Obviously this isn't going to be the be all and end all of
159 solutions but it works for me.
160  
161 Essentially all it does is export a method into your namespace that
162 looks through a search path for .pm files and turn those into class names.
163  
164 Optionally it instantiates those classes for you.
165  
166 =head1 ADVANCED USAGE
167  
168 Alternatively, if you don't want to use 'plugins' as the method ...
169  
170 package MyClass;
171 use Module::Pluggable sub_name => 'foo';
172  
173  
174 and then later ...
175  
176 my @plugins = $mc->foo();
177  
178  
179 Or if you want to look in another namespace
180  
181 package MyClass;
182 use Module::Pluggable search_path => ['Acme::MyClass::Plugin', 'MyClass::Extend'];
183  
184 or directory
185  
186 use Module::Pluggable search_dirs => ['mylibs/Foo'];
187  
188 If you I<only> want to search those C<search_dirs>, provide a true value for
189 C<search_dirs_strict>.
190  
191 Or if you want to instantiate each plugin rather than just return the name
192  
193 package MyClass;
194 use Module::Pluggable instantiate => 'new';
195  
196 and then
197  
198 # whatever is passed to 'plugins' will be passed
199 # to 'new' for each plugin
200 my @plugins = $mc->plugins(@options);
201  
202  
203 alternatively you can just require the module without instantiating it
204  
205 package MyClass;
206 use Module::Pluggable require => 1;
207  
208 since requiring automatically searches inner packages, which may not be desirable, you can turn this off
209  
210  
211 package MyClass;
212 use Module::Pluggable require => 1, inner => 0;
213  
214  
215 You can limit the plugins loaded using the except option, either as a string,
216 array ref or regex
217  
218 package MyClass;
219 use Module::Pluggable except => 'MyClass::Plugin::Foo';
220  
221 or
222  
223 package MyClass;
224 use Module::Pluggable except => ['MyClass::Plugin::Foo', 'MyClass::Plugin::Bar'];
225  
226 or
227  
228 package MyClass;
229 use Module::Pluggable except => qr/^MyClass::Plugin::(Foo|Bar)$/;
230  
231  
232 and similarly for only which will only load plugins which match.
233  
234 Remember you can use the module more than once
235  
236 package MyClass;
237 use Module::Pluggable search_path => 'MyClass::Filters', sub_name => 'filters';
238 use Module::Pluggable search_path => 'MyClass::Plugins', sub_name => 'plugins';
239  
240 and then later ...
241  
242 my @filters = $self->filters;
243 my @plugins = $self->plugins;
244  
245 =head1 PLUGIN SEARCHING
246  
247 Every time you call 'plugins' the whole search path is walked again. This allows
248 for dynamically loading plugins even at run time. However this can get expensive
249 and so if you don't expect to want to add new plugins at run time you could do
250  
251  
252 package Foo;
253 use strict;
254 use Module::Pluggable sub_name => '_plugins';
255  
256 our @PLUGINS;
257 sub plugins { @PLUGINS ||= shift->_plugins }
258 1;
259  
260 =head1 INNER PACKAGES
261  
262 If you have, for example, a file B<lib/Something/Plugin/Foo.pm> that
263 contains package definitions for both C<Something::Plugin::Foo> and
264 C<Something::Plugin::Bar> then as long as you either have either
265 the B<require> or B<instantiate> option set then we'll also find
266 C<Something::Plugin::Bar>. Nifty!
267  
268 =head1 OPTIONS
269  
270 You can pass a hash of options when importing this module.
271  
272 The options can be ...
273  
274 =head2 sub_name
275  
276 The name of the subroutine to create in your namespace.
277  
278 By default this is 'plugins'
279  
280 =head2 search_path
281  
282 An array ref of namespaces to look in.
283  
284 search_path => ['MyApp::Plugin', 'MyApp::Extension']
285  
286 Alternatively, you can pass a hash ref where each key is a namespace prefix and
287 each value is a hash ref of option overrides for that path. The per-path
288 options are merged on top of the global options, so you only need to
289 specify what differs.
290  
291 search_path => {
292 'MyApp::Plugin' => { max_depth => 2 },
293 'MyApp::Extension' => { max_depth => 3, instantiate => 'new' },
294 }
295  
296 If you want plugins to be in a particular order, use an array ref of mixed
297 plain strings and single-key hash refs instead. Order is preserved; only the
298 hash ref entries carry per-path option overrides.
299  
300 search_path => [
301 'MyApp::Plugin',
302 { 'MyApp::Extension' => { max_depth => 3, instantiate => 'new' } },
303 ]
304  
305 Results from all paths are combined and returned in a single list. Class
306 names are deduplicated across paths; if any path uses C<instantiate> its
307 objects are included as-is alongside any class names from other paths.
308  
309 =head2 search_dirs
310  
311 An array ref of directories to look in before @INC.
312  
313 =head2 instantiate
314  
315 Call this method on the class. In general this will probably be 'new'
316 but it can be whatever you want. Whatever arguments are passed to 'plugins'
317 will be passed to the method.
318  
319 The default is 'undef' i.e just return the class name.
320  
321 =head2 require
322  
323 Just require the class, don't instantiate (overrides 'instantiate');
324  
325 =head2 inner
326  
327 If set to 0 will B<not> search inner packages.
328 If set to 1 will override C<require>.
329  
330 =head2 only
331  
332 Takes a string, array ref or regex describing the names of the only plugins to
333 return. Whilst this may seem perverse ... well, it is. But it also
334 makes sense. Trust me.
335  
336 =head2 except
337  
338 Similar to C<only> it takes a description of plugins to exclude
339 from returning. This is slightly less perverse.
340  
341 =head2 package
342  
343 This is for use by extension modules which build on C<Module::Pluggable>:
344 passing a C<package> option allows you to place the plugin method in a
345 different package other than your own.
346  
347 =head2 file_regex
348  
349 By default C<Module::Pluggable> only looks for I<.pm> files.
350  
351 By supplying a new C<file_regex> then you can change this behaviour e.g
352  
353 file_regex => qr/\.plugin$/
354  
355 =head2 include_editor_junk
356  
357 By default C<Module::Pluggable> ignores files that look like they were
358 left behind by editors. Currently this means files ending in F<~> (~),
359 the extensions F<.swp> or F<.swo>, or files beginning with F<.#>.
360  
361 Setting C<include_editor_junk> changes C<Module::Pluggable> so it does
362 not ignore any files it finds.
363  
364 =head2 follow_symlinks
365  
366 Whether, when searching directories, to follow symlinks.
367  
368 Defaults to 1 i.e do follow symlinks.
369  
370 =head2 min_depth, max_depth
371  
372 This will allow you to set what 'depth' of plugin will be allowed.
373  
374 So, for example, C<MyClass::Plugin::Foo> will have a depth of 3 and
375 C<MyClass::Plugin::Foo::Bar> will have a depth of 4 so to only get the former
376 (i.e C<MyClass::Plugin::Foo>) do
377  
378 package MyClass;
379 use Module::Pluggable max_depth => 3;
380  
381 and to only get the latter (i.e C<MyClass::Plugin::Foo::Bar>)
382  
383 package MyClass;
384 use Module::Pluggable min_depth => 4;
385  
386 =head2 sort_results
387  
388 Controls the order plugin names (or objects) are returned in. One of:
389  
390 =over 4
391  
392 =item * C<'alpha'> (the default) - sorted alphabetically by package name.
393  
394 =item * C<'path'> - returned in search-path discovery order: all plugins
395 under the first C<search_path> entry, then the second, and so on.
396  
397 =item * C<0> - no sort is applied at all. Fastest option, for callers that
398 will sort or otherwise order the results themselves.
399  
400 =back
401  
402 package MyClass;
403 use Module::Pluggable sort_results => 'path';
404  
405 =head1 TRIGGERS
406  
407 Various triggers can also be passed in to the options.
408  
409 If any of these triggers return 0 then the plugin will not be returned.
410  
411 =head2 before_require <plugin>
412  
413 Gets passed the plugin name.
414  
415 If 0 is returned then this plugin will not be required either.
416  
417 =head2 on_require_error <plugin> <err>
418  
419 Gets called when there's an error on requiring the plugin.
420  
421 Gets passed the plugin name and the error.
422  
423 The default on_require_error handler is to C<carp> the error and return 0.
424  
425 =head2 after_require <plugin>
426  
427 Gets passed the plugin name.
428  
429 If 0 is returned then this plugin will be required but not instantiated or
430 returned as a plugin.
431  
432 =head2 before_instantiate <plugin>
433  
434 Gets passed the plugin name.
435  
436 If 0 is returned, then instantiation will not occur.
437  
438 =head2 on_instantiate_error <plugin> <err>
439  
440 Gets called when there's an error on instantiating the plugin.
441  
442 Gets passed the plugin name and the error.
443  
444 The default on_instantiate_error handler is to C<carp> the error and return 0.
445  
446 =head2 after_instantiate <plugin> <obj>
447  
448 Gets passed the plugin name and the object that was instentiated.
449 i.e. the return value of C<Plugin->new()>
450  
451 The return value must be <obj> and will be returned AS the plugin.
452 If the return value is not true, then nothing will be returned as a plugin.
453  
454 =head1 METHODs
455  
456 =head2 search_path
457  
458 The method C<search_path> is exported into you namespace as well.
459 You can call that at any time to change or replace the
460 search_path.
461  
462 $self->search_path( add => "New::Path" ); # add
463 $self->search_path( new => "New::Path" ); # replace
464  
465 =head1 BEHAVIOUR UNDER TEST ENVIRONMENT
466  
467 In order to make testing reliable we exclude anything not from blib if blib.pm is
468 in %INC.
469  
470 However if the module being tested used another module that itself used C<Module::Pluggable>
471 then the second module would fail. This was fixed by checking to see if the caller
472 had (^|/)blib/ in their filename.
473  
474 There's an argument that this is the wrong behaviour and that modules should explicitly
475 trigger this behaviour but that particular code has been around for 7 years now and I'm
476 reluctant to change the default behaviour.
477  
478 You can now (as of version 4.1) force Module::Pluggable to look outside blib in a test environment by doing either
479  
480 require Module::Pluggable;
481 $Module::Pluggable::FORCE_SEARCH_ALL_PATHS = 1;
482 import Module::Pluggable;
483  
484 or
485  
486 use Module::Pluggable force_search_all_paths => 1;
487  
488 =head1 @INC hooks and App::FatPacker
489  
490 If a module's @INC has a hook and that hook is an object which has a C<files()> method then we will
491 try and require those files too. See C<t/26inc_hook.t> for an example.
492  
493 This has allowed L<App::FatPacker> (as of version 0.10.0) to provide support for Module::Pluggable.
494  
495 This should also, theoretically, allow someone to modify PAR to do the same thing.
496  
497 =head1 Module::Require recommended
498  
499 Up until version 5.2 L<Module::Pluggable> used a string C<eval> to require plugins.
500  
501 This has now been changed to optionally use L<Module::Runtime> and it's C<require_module> method when
502 available and fall back to using a path based C<require> when not.
503  
504 It's recommended, but not required, that you install Module::Runtime.
505  
506 =head1 FUTURE PLANS
507  
508 This does everything I need and I can't really think of any other
509 features I want to add. Famous last words of course (not least
510 because we're up to version 5.0 at the time of writing).
511  
512 However suggestions (and patches) are always welcome.
513  
514 =head1 DEVELOPMENT
515  
516 The master repo for this module is at
517  
518 https://github.com/simonwistow/Module-Pluggable
519  
520 =head1 AUTHOR
521  
522 Simon Wistow <simon@thegestalt.org>
523  
524 =head1 COPYING
525  
526 Copyright, 2006 Simon Wistow
527  
528 Distributed under the same terms as Perl itself.
529  
530 =head1 BUGS
531  
532 None known.
533  
534 =head1 SEE ALSO
535  
536 L<File::Spec>, L<File::Find>, L<File::Basename>, L<Class::Factory::Util>, L<Module::Pluggable::Ordered>
537  
538 =cut
539  
540  
« Module/Pluggable/Object.pm ↑ Summary Devel/InnerPackage.pm »