Module/Pluggable/Object.pm

filter: Statementstmt 94.1% 258 / 274 Branchbran 77.5% 121 / 156 Conditioncond 75.7% 72 / 95 MC/DCmcdc 49.0% 27 / 55 Subroutinesub 88.8% 24 / 27 Podpod 100% 8 / 8 totaltotal 82.9% 510 / 615 CC 125 SCAR 32.2
CC
125
Cov
86%
plugins
54.4
search_paths
32.0
_is_legit
16.0
CRAP 25.0
↑ Summary Module/Pluggable.pm »
1 package Module::Pluggable::Object;
2  
3 43 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 43 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 43 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 43 ▶use File::Spec::Functions qw(splitdir catdir curdir catfile abs2rel);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 43 ▶use Carp qw(croak carp confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 43 ▶use Devel::InnerPackage;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 43 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 43 ▶use if $] > 5.017, 'deprecate';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 43 our $VERSION = '5.3';
14  
15 43 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=9.7
16 43 eval { require Module::Runtime };
17 43 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 43 43
18 43 Module::Runtime->import('require_module');
19 } else {
20 0 ▶ *require_module = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 0 my $module = shift;
22 0 my $path = $module . ".pm";
23 0 $path =~ s{::}{/}g;
24 0 require $path;
25 0 };
26 }
27 }
28  
29  
30 =head1 NAME
31  
32 Module::Pluggable::Object - the underlying plugin-finding object used by Module::Pluggable
33  
34 =head1 SYNOPSIS
35  
36 package MyClass;
37 use Module::Pluggable::Object;
38  
39 my $finder = Module::Pluggable::Object->new(
40 package => __PACKAGE__,
41 search_path => ['MyClass::Plugin'],
42 require => 1,
43 );
44  
45 my @plugins = $finder->plugins;
46 print "Found: ", join(", ", @plugins), "\n";
47  
48 =head1 DESCRIPTION
49  
50 C<Module::Pluggable::Object> is the engine underneath L<Module::Pluggable>. It
51 walks a set of search paths (namespaces and directories) looking for C<.pm>
52 files, maps their paths to package names, and optionally C<require>s or
53 instantiates them.
54  
55 Most of the time you should use L<Module::Pluggable> directly, which wraps
56 this class and exports a C<plugins()> method into your namespace. Use this
57 class directly when you need more control:
58  
59 =over 4
60  
61 =item * You want to create multiple independent finders in the same package.
62  
63 =item * You want to subclass or decorate the finder object.
64  
65 =item * You want to store the finder and call C<plugins()> on it explicitly
66 rather than through an exported method.
67  
68 =item * You are building your own higher-level abstraction on top of the
69 plugin-discovery machinery.
70  
71 =back
72  
73 =head1 CONSTRUCTOR
74  
75 =head2 new(%opts)
76  
77 my $finder = Module::Pluggable::Object->new(%opts);
78  
79 Creates and returns a new finder object. All options are passed as a flat
80 hash; they are stored on the object and consulted by L</plugins>. See
81 L</OPTIONS> below for the full list.
82  
83 =cut
84  
85 69 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
86 69 my $class = shift;
87 69 my %opts = @_;
88  
89 69 return bless \%opts, $class;
90  
91 }
92  
93 =head1 METHODS
94  
95 =head2 plugins
96  
97 my @plugins = $finder->plugins; # list of class names
98 my @objects = $finder->plugins(@args); # instances when 'instantiate' is set
99  
100 The main entry point. Walks C<search_path> across C<search_dirs> (and C<@INC>)
101 and returns either a sorted list of fully-qualified package names or, if
102 C<instantiate> is set, a list of objects created by calling the named method
103 on each plugin class.
104  
105 Arguments passed to C<plugins()> are forwarded to the C<instantiate> method
106 on each plugin class.
107  
108 The full search is repeated on every call, which allows plugins installed
109 after startup to be discovered at run time. If you do not need dynamic
110 discovery and want to pay the I/O cost only once, memoize the result yourself:
111  
112 our @PLUGINS;
113 sub plugins { @PLUGINS ||= shift->_finder->plugins }
114  
115 =cut
116  
117 ### Eugggh, this code smells
118 ### This is what happens when you keep adding patches
119 ### *sigh*
120  
121 # Normalises the three supported forms of 'search_path' into an ordered
122 # list of [$namespace, \%per_path_opts] pairs:
123 # "Foo::Plugin" -> plain scalar
124 # ["Foo::Plugin", "Bar::Plugin"] -> plain arrayref
125 # { "Foo::Plugin" => { max_depth => 2 }, ... } -> hashref, sorted for determinism
126 # ["Foo::Plugin", { "Bar::Plugin" => { ... } }] -> mixed arrayref, preserves order
127 91 ▶sub _normalise_search_path {
Subroutine _normalise_search_path: called CC=10 SCAR=23.4
128 91 my ($sp) = @_;
129 91 ▶ return () unless defined $sp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sp 25 66 91
130 66 ▶ return ([$sp, {}]) unless ref $sp;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sp 21 45 66
131 45 ▶ if (ref($sp) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sp eq "HASH") 4 41 45
132 4 ▶ return map { [$_, $sp->{$_} || {}] } sort keys %$sp;
Condition50%1/2
$sp->{$_} || {}
l!l
70
MC/DC0%0/1
$sp->{$_} || {}
$sp->{$_}
Truth table50%1/2
A
A= $sp->{$_}
Aresult
11
00
133 }
134 41 ▶ if (ref($sp) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sp eq "ARRAY") 41 0 41
135 41 my @out;
136 41 for my $item (@$sp) {
137 44 ▶ if (!ref($item)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $item) { } 40 4 44
elsif (ref $item eq 'HASH') { } 4 0 4
138 40 push @out, [$item, {}];
139 } elsif (ref($item) eq 'HASH') {
140 4 ▶ push @out, map { [$_, $item->{$_} || {}] } sort keys %$item;
Condition50%1/2
$item->{$_} || {}
l!l
40
MC/DC0%0/1
$item->{$_} || {}
$item->{$_}
Truth table50%1/2
A
A= $item->{$_}
Aresult
11
00
141 }
142 }
143 41 return @out;
144 }
145 0 return ();
146 }
147  
148 91 ▶sub plugins {
Subroutine plugins: called CC=41 SCAR=40.0
Pod documented
149 91 my $self = shift;
150 91 my @args = @_;
151  
152 91 my $pkg = $self->{'package'};
153  
154 # Normalise search_path, then apply the default
155 91 my @path_specs = _normalise_search_path($self->{'search_path'});
156 91 ▶ @path_specs = ([$pkg . '::Plugin', {}]) unless @path_specs;
Branch: 1 branch
BranchTrueFalseTotal
unless @path_specs 25 66 91
157  
158 # If any path carries per-path option overrides, dispatch each path through
159 # its own sub-finder and merge results in spec order, deduplicating class names.
160 91 ▶ if (grep { %{$_->[1]} } @path_specs) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {%{$_->[1];};} @path_specs) 4 87 91
161 4 my @results;
162 4 for my $spec (@path_specs) {
163 8 my ($path, $per_path) = @$spec;
164 8 my $sub = ref($self)->new(%$self, %$per_path, search_path => [$path]);
165 8 push @results, $sub->plugins(@args);
166 }
167 4 my %seen;
168 4 ▶ return grep { ref($_) ? 1 : !$seen{$_}++ } @results;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 11 11
169 }
170  
171 # No per-path overrides: restore as a simple arrayref and continue as normal
172 87 $self->{'search_path'} = [map { $_->[0] } @path_specs];
173  
174 # override 'require'
175 87 ▶ $self->{'require'} = 1 if $self->{'inner'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"inner"} 3 84 87
176  
177 87 my $filename = $self->{'filename'};
178  
179 # Get the exception params instantiated
180 87 $self->_setup_exceptions;
181  
182 # automatically turn a scalar search_dirs into an arrayref
183 $self->{'search_dirs'} = [ $self->{'search_dirs'} ]
184 87 ▶ if exists $self->{'search_dirs'} && !ref($self->{'search_dirs'});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"search_dirs"} and not ref $self->{"search_dirs"} 4 83 87
Condition100%3/3
exists $self->{"search_dirs"} and not ref $self->{"search_dirs"}
!ll&&!rl&&r
72114
MC/DC100%2/2
exists $self->{"search_dirs"} and not ref $self->{"search_dirs"}
exists $self->{"search_dirs"} not ref $self->{"search_dirs"}
Truth table100%3/3
A and not(B)
A= exists $self->{"search_dirs"}B= not ref $self->{"search_dirs"}
ABresult
0X0
100
111
185  
186 # default error handler
187 0 ▶ $self->{'on_require_error'} ||= sub { my ($plugin, $err) = @_; carp "Couldn't require $plugin : $err"; return 0 };
Subroutine __ANON__: not called CC=1 SCAR=1.7
Condition66%2/3
$self->{"on_require_error"} ||= sub { my($plugin, $err) = @_; carp("Couldn't require $plugin : $err"); return 0; }
l!l&&r!l&&!r
28590
MC/DC0%0/2
$self->{"on_require_error"} ||= sub { my($plugin, $err) = @_; carp("Couldn't require $plugin : $err"); return 0; }
$self->{"on_require_error"} sub { my($plugin, $err) = @_; carp("Couldn't require $plugin : $err"); return 0; }
Truth table66%2/3
A ||= B
A= $self->{"on_require_error"}B= sub { my($plugin, $err) = @_; carp("Couldn't require $plugin : $err"); return 0; }
ABresult
1X1
011
000
188 0 ▶ $self->{'on_instantiate_error'} ||= sub { my ($plugin, $err) = @_; carp "Couldn't instantiate $plugin: $err"; return 0 };
Subroutine __ANON__: not called CC=1 SCAR=1.7
Condition66%2/3
$self->{"on_instantiate_error"} ||= sub { my($plugin, $err) = @_; carp("Couldn't instantiate ${plugin}: $err"); return 0; }
l!l&&r!l&&!r
24630
MC/DC0%0/2
$self->{"on_instantiate_error"} ||= sub { my($plugin, $err) = @_; carp("Couldn't instantiate ${plugin}: $err"); return 0; }
$self->{"on_instantiate_error"} sub { my($plugin, $err) = @_; carp("Couldn't instantiate ${plugin}: $err"); return 0; }
Truth table66%2/3
A ||= B
A= $self->{"on_instantiate_error"}B= sub { my($plugin, $err) = @_; carp("Couldn't instantiate ${plugin}: $err"); return 0; }
ABresult
1X1
011
000
189  
190 # before and after instantiation hooks
191 87 ▶ $self->{'before_instantiate'} ||= sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$self->{"before_instantiate"} ||= sub { 1; }
l!l&&r!l&&!r
24630
MC/DC0%0/2
$self->{"before_instantiate"} ||= sub { 1; }
$self->{"before_instantiate"} sub { 1; }
Truth table66%2/3
A ||= B
A= $self->{"before_instantiate"}B= sub { 1; }
ABresult
1X1
011
000
192 87 ▶ $self->{'after_instantiate'} ||= sub { return $_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$self->{"after_instantiate"} ||= sub { return $_[1]; }
l!l&&r!l&&!r
24630
MC/DC0%0/2
$self->{"after_instantiate"} ||= sub { return $_[1]; }
$self->{"after_instantiate"} sub { return $_[1]; }
Truth table66%2/3
A ||= B
A= $self->{"after_instantiate"}B= sub { return $_[1]; }
ABresult
1X1
011
000
193  
194 # default whether to follow symlinks
195 # because the default behavior is changed in the Perl-CORE module File::Find VERSION >= '1.39',
196 # in lower versions of File::Find, 'follow_symlinks' is (independent from the callers setting of
197 # 'follow_symlinks') hardcoded set to 0 on Windows so we force File::Find to fall back to the old
198 # behavior, if not otherwise told
199 87 ▶ $self->{'follow_symlinks'} = 0 if ($File::Find::VERSION >= '1.39' && $^O eq 'MSWin32' && ! exists $self->{'follow_symlinks'} );
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::VERSION >= "1.39" and $^O eq "MSWin32" and not exists $self->{"follow_symlinks"} 0 87 87
Condition33%2/6
$File::Find::VERSION >= "1.39" and $^O eq "MSWin32" and not exists $self->{"follow_symlinks"}
!ll&&!rl&&r
8700
$File::Find::VERSION >= "1.39" and $^O eq "MSWin32"
!ll&&!rl&&r
0870
MC/DC0%0/3
$File::Find::VERSION >= "1.39" and $^O eq "MSWin32" and not exists $self->{"follow_symlinks"}
$File::Find::VERSION >= "1.39" $^O eq "MSWin32" not exists $self->{"follow_symlinks"}
Truth table25%1/4
A and B and not(C)
A= $File::Find::VERSION >= "1.39"B= $^O eq "MSWin32"C= not exists $self->{"follow_symlinks"}
ABCresult
0XX0
10X0
1100
1111
200 87 ▶ $self->{'follow_symlinks'} = 1 unless exists $self->{'follow_symlinks'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"follow_symlinks"} 64 23 87
201  
202 # check to see if we're running under test
203 0 ▶ my @SEARCHDIR = exists $INC{"blib.pm"} && defined $filename && $filename =~ m!(^|/)blib/! && !$self->{'force_search_all_paths'} ? grep {/blib/} @INC : @INC;
Branch: 1 branch
BranchTrueFalseTotal
exists $INC{'blib.pm'} && defined $filename && $filename =~ m[(^|/)blib/] && !$self->{'force_search_all_paths'} ? : 0 87 87
Condition55%5/9
exists $INC{"blib.pm"} and defined $filename and $filename =~ m[(^|/)blib/] and not $self->{"force_search_all_paths"}
!ll&&!rl&&r
8700
exists $INC{"blib.pm"} and defined $filename and $filename =~ m[(^|/)blib/]
!ll&&!rl&&r
11760
exists $INC{"blib.pm"} and defined $filename
!ll&&!rl&&r
01176
MC/DC0%0/4
exists $INC{"blib.pm"} and defined $filename and $filename =~ m[(^|/)blib/] and not $self->{"force_search_all_paths"}
exists $INC{"blib.pm"} defined $filename $filename =~ m[(^|/)blib/] not $self->{"force_search_all_paths"}
Truth table40%2/5
A and B and C and not(D)
A= exists $INC{"blib.pm"}B= defined $filenameC= $filename =~ m[(^|/)blib/]D= not $self->{"force_search_all_paths"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
204  
205 87 ▶ if ($self->{'search_dirs_strict'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"search_dirs_strict"}) 1 86 87
206 die "search_dirs_strict: makes no sense without setting search_dirs"
207 1 ▶ unless $self->{'search_dirs'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"search_dirs"} 0 1 1
208  
209 1 @SEARCHDIR = ();
210 }
211  
212 # add any search_dir params
213 87 ▶ unshift @SEARCHDIR, @{$self->{'search_dirs'}} if defined $self->{'search_dirs'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"search_dirs"} 15 72 87
214  
215 # set our @INC up to include and prefer our search_dirs if necessary
216 87 my @tmp = @INC;
217 87 ▶ unshift @tmp, @{$self->{'search_dirs'} || []};
Condition100%2/2
$self->{'search_dirs'} || []
l!l
1572
MC/DC100%1/1
$self->{'search_dirs'} || []
$self->{'search_dirs'}
Truth table100%2/2
A
A= $self->{'search_dirs'}
Aresult
11
00
218 87 ▶ local @INC = @tmp if defined $self->{'search_dirs'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"search_dirs"} 15 72 87
219  
220 87 my @plugins = $self->search_directories(@SEARCHDIR);
221 86 push(@plugins, $self->handle_inc_hooks($_, @SEARCHDIR)) for @{$self->{'search_path'}};
222 86 push(@plugins, $self->handle_innerpackages($_)) for @{$self->{'search_path'}};
223  
224 # return blank unless we've found anything
225 86 ▶ return () unless @plugins;
Branch: 1 branch
BranchTrueFalseTotal
unless @plugins 5 81 86
226  
227 # remove duplicates, keeping track of discovery order for 'sort_results'
228 81 my (@dedupe, %plugins);
229 81 for(@plugins) {
230 219 ▶ next unless $self->_is_legit($_);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_legit($_) 0 219 219
231 219 ▶ next if $plugins{$_}++;
Branch: 1 branch
BranchTrueFalseTotal
if $plugins{$_}++ 71 148 219
232 148 push @dedupe, $_;
233 }
234  
235 81 ▶ my $sort_results = exists $self->{'sort_results'} ? $self->{'sort_results'} : 'alpha';
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'sort_results'} ? : 0 81 81
236 81 ▶ my @ordered = $sort_results eq 'path' ? @dedupe
Branch: 2 branches
BranchTrueFalseTotal
$sort_results eq 'path' ? : 0 81 81
!$sort_results ? : 0 81 81
237 : !$sort_results ? keys %plugins
238 : sort keys %plugins;
239  
240 # are we instantiating or requiring?
241 81 ▶ if (defined $self->{'instantiate'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'instantiate'}) { } 3 78 81
242 3 my $method = $self->{'instantiate'};
243 3 my @objs = ();
244 3 foreach my $package (@ordered) {
245 3 ▶ next unless $package->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless $package->can($method) 1 2 3
246 2 ▶ $self->{'before_instantiate'}->($package)
Branch: 1 branch
BranchTrueFalseTotal
$self->{"before_instantiate"}->($package) or next 2 0 2
247 or next;
248 2 my $obj = eval { $package->$method(@_) }; # We dont actually care what ->$method() returns
249 2 ▶ $self->{'on_instantiate_error'}->($package, $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
250 2 ▶ if ($obj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj) 2 0 2
251 2 ▶ $obj = $self->{'after_instantiate'}->($package,$obj)
Branch: 1 branch
BranchTrueFalseTotal
$obj = $self->{"after_instantiate"}->($package, $obj) or next 2 0 2
252 or next; # Again, we dont actually care if we get a blessed reference or not
253 2 push @objs, $obj;
254 }
255 }
256 3 return @objs;
257 } else {
258 # no? just return the names
259 78 my @objs = @ordered;
260 78 return @objs;
261 }
262 }
263  
264 87 ▶sub _setup_exceptions {
Subroutine _setup_exceptions: called CC=9 SCAR=22.0
265 87 my $self = shift;
266  
267 87 my %only;
268 my %except;
269 87 my $only;
270 87 my $except;
271  
272 87 ▶ if (defined $self->{'only'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"only"}) 13 74 87
273 13 ▶ if (ref($self->{'only'}) eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $self->{'only'} eq 'ARRAY') { } 4 9 13
elsif (ref $self->{'only'} eq 'Regexp') { } 5 4 9
elsif (ref $self->{'only'} eq '') { } 4 0 4
274 4 %only = map { $_ => 1 } @{$self->{'only'}};
275 } elsif (ref($self->{'only'}) eq 'Regexp') {
276 5 $only = $self->{'only'}
277 } elsif (ref($self->{'only'}) eq '') {
278 4 $only{$self->{'only'}} = 1;
279 }
280 }
281  
282  
283 87 ▶ if (defined $self->{'except'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"except"}) 12 75 87
284 12 ▶ if (ref($self->{'except'}) eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $self->{'except'} eq 'ARRAY') { } 4 8 12
elsif (ref $self->{'except'} eq 'Regexp') { } 4 4 8
elsif (ref $self->{'except'} eq '') { } 4 0 4
285 4 %except = map { $_ => 1 } @{$self->{'except'}};
286 } elsif (ref($self->{'except'}) eq 'Regexp') {
287 4 $except = $self->{'except'}
288 } elsif (ref($self->{'except'}) eq '') {
289 4 $except{$self->{'except'}} = 1;
290 }
291 }
292 87 $self->{_exceptions}->{only_hash} = \%only;
293 87 $self->{_exceptions}->{only} = $only;
294 87 $self->{_exceptions}->{except_hash} = \%except;
295 87 $self->{_exceptions}->{except} = $except;
296  
297 }
298  
299 497 ▶sub _is_legit {
Subroutine _is_legit: called CC=16 SCAR=27.7
300 497 my $self = shift;
301 497 my $plugin = shift;
302 497 ▶ my %only = %{$self->{_exceptions}->{only_hash}||{}};
Condition50%1/2
$self->{'_exceptions'}{'only_hash'} || {}
l!l
4970
MC/DC0%0/1
$self->{'_exceptions'}{'only_hash'} || {}
$self->{'_exceptions'}{'only_hash'}
Truth table50%1/2
A
A= $self->{'_exceptions'}{'only_hash'}
Aresult
11
00
303 497 ▶ my %except = %{$self->{_exceptions}->{except_hash}||{}};
Condition50%1/2
$self->{'_exceptions'}{'except_hash'} || {}
l!l
4970
MC/DC0%0/1
$self->{'_exceptions'}{'except_hash'} || {}
$self->{'_exceptions'}{'except_hash'}
Truth table50%1/2
A
A= $self->{'_exceptions'}{'except_hash'}
Aresult
11
00
304 497 my $only = $self->{_exceptions}->{only};
305 497 my $except = $self->{_exceptions}->{except};
306 497 my $depth = () = split '::', $plugin, -1;
307  
308 497 ▶ return 0 if (keys %only && !$only{$plugin} );
Branch: 1 branch
BranchTrueFalseTotal
if keys %only and not $only{$plugin} 16 481 497
Condition100%3/3
keys %only and not $only{$plugin}
!ll&&!rl&&r
4651616
MC/DC100%2/2
keys %only and not $only{$plugin}
keys %only not $only{$plugin}
Truth table100%3/3
A and not(B)
A= keys %onlyB= not $only{$plugin}
ABresult
0X0
100
111
309 481 ▶ return 0 unless (!defined $only || $plugin =~ m!$only! );
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $only or $plugin =~ /$only/ 9 472 481
Condition100%3/3
not defined $only or $plugin =~ /$only/
l!l&&r!l&&!r
46489
MC/DC100%2/2
not defined $only or $plugin =~ /$only/
not defined $only $plugin =~ /$only/
Truth table100%3/3
not(A) or B
A= not defined $onlyB= $plugin =~ /$only/
ABresult
1X1
011
000
310  
311 472 ▶ return 0 if (keys %except && $except{$plugin} );
Branch: 1 branch
BranchTrueFalseTotal
if keys %except and $except{$plugin} 8 464 472
Condition100%3/3
keys %except and $except{$plugin}
!ll&&!rl&&r
432328
MC/DC100%2/2
keys %except and $except{$plugin}
keys %except $except{$plugin}
Truth table100%3/3
A and B
A= keys %exceptB= $except{$plugin}
ABresult
0X0
100
111
312 464 ▶ return 0 if (defined $except && $plugin =~ m!$except! );
Branch: 1 branch
BranchTrueFalseTotal
if defined $except and $plugin =~ /$except/ 4 460 464
Condition100%3/3
defined $except and $plugin =~ /$except/
!ll&&!rl&&r
444164
MC/DC100%2/2
defined $except and $plugin =~ /$except/
defined $except $plugin =~ /$except/
Truth table100%3/3
A and B
A= defined $exceptB= $plugin =~ /$except/
ABresult
0X0
100
111
313  
314 460 ▶ return 0 if defined $self->{max_depth} && $depth>$self->{max_depth};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"max_depth"} and $depth > $self->{"max_depth"} 11 449 460
Condition100%3/3
defined $self->{"max_depth"} and $depth > $self->{"max_depth"}
!ll&&!rl&&r
4113811
MC/DC100%2/2
defined $self->{"max_depth"} and $depth > $self->{"max_depth"}
defined $self->{"max_depth"} $depth > $self->{"max_depth"}
Truth table100%3/3
A and B
A= defined $self->{"max_depth"}B= $depth > $self->{"max_depth"}
ABresult
0X0
100
111
315 449 ▶ return 0 if defined $self->{min_depth} && $depth<$self->{min_depth};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"min_depth"} and $depth < $self->{"min_depth"} 3 446 449
Condition100%3/3
defined $self->{"min_depth"} and $depth < $self->{"min_depth"}
!ll&&!rl&&r
44423
MC/DC100%2/2
defined $self->{"min_depth"} and $depth < $self->{"min_depth"}
defined $self->{"min_depth"} $depth < $self->{"min_depth"}
Truth table100%3/3
A and B
A= defined $self->{"min_depth"}B= $depth < $self->{"min_depth"}
ABresult
0X0
100
111
316  
317 446 ▶ return 0 if $plugin =~ /(^|::).AppleDouble/;
Branch: 1 branch
BranchTrueFalseTotal
if $plugin =~ /(^|::).AppleDouble/ 1 445 446
318  
319 445 return 1;
320 }
321  
322 =head2 search_directories(@dirs)
323  
324 my @plugins = $finder->search_directories(@INC);
325  
326 Iterates over C<@dirs> and delegates to L</search_paths> for each one.
327 Normally called internally by L</plugins>, but exposed so subclasses can
328 override the top-level directory loop.
329 =cut
330  
331 87 ▶sub search_directories {
Subroutine search_directories: called CC=2 SCAR=6.9
Pod documented
332 87 my $self = shift;
333 87 my @SEARCHDIR = @_;
334  
335 87 my @plugins;
336 # go through our @INC
337 87 foreach my $dir (@SEARCHDIR) {
338 695 push @plugins, $self->search_paths($dir);
339 }
340 86 return @plugins;
341 }
342  
343 =head2 search_paths($dir)
344  
345 my @plugins = $finder->search_paths($dir);
346  
347 For a single base directory, iterates over C</search_path> namespaces, locates
348 C<.pm> files under the corresponding subdirectory, converts file paths to
349 package names, and calls L</handle_finding_plugin> for each candidate.
350  
351 Handles case-insensitive filesystems by reading the C<package> declaration
352 out of the file when the file name is all-lower or all-upper case.
353  
354 =cut
355  
356 695 ▶sub search_paths {
Subroutine search_paths: called CC=26 SCAR=34.6
Pod documented
357 695 my $self = shift;
358 695 my $dir = shift;
359 695 my @plugins;
360  
361 695 ▶ my $file_regex = $self->{'file_regex'} || qr/\.pm$/;
Condition100%2/2
$self->{'file_regex'} || qr/\.pm$/
l!l
16679
MC/DC100%1/1
$self->{'file_regex'} || qr/\.pm$/
$self->{'file_regex'}
Truth table100%2/2
A
A= $self->{'file_regex'}
Aresult
11
00
362  
363  
364 # and each directory in our search path
365 695 foreach my $searchpath (@{$self->{'search_path'}}) {
366 # create the search directory in a cross platform goodness way
367 712 my $sp = catdir($dir, (split /::/, $searchpath));
368  
369 # if it doesn't exist or it's not a dir then skip it
370 712 ▶ next unless ( -e $sp && -d _ ); # Use the cached stat the second time
Branch: 1 branch
BranchTrueFalseTotal
unless -e $sp and -d _ 610 102 712
Condition66%2/3
-e $sp and -d _
!ll&&!rl&&r
6100102
MC/DC50%1/2
-e $sp and -d _
-e $sp -d _
Truth table66%2/3
A and B
A= -e $spB= -d _
ABresult
0X0
100
111
371  
372 102 my @files = $self->find_files($sp);
373  
374 # foreach one we've found
375 102 foreach my $file (@files) {
376 # untaint the file; accept .pm only
377 222 ▶ next unless ($file) = ($file =~ /(.*$file_regex)$/);
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) = $file =~ /(.*$file_regex)$/ 0 222 222
378 # parse the file to get the name
379 222 my ($name, $directory, $suffix) = fileparse($file, $file_regex);
380  
381 222 ▶ next if (!$self->{include_editor_junk} && $self->_is_editor_junk($name));
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"include_editor_junk"} and $self->_is_editor_junk($name) 1 221 222
Condition100%3/3
not $self->{"include_editor_junk"} and $self->_is_editor_junk($name)
!ll&&!rl&&r
32181
MC/DC100%2/2
not $self->{"include_editor_junk"} and $self->_is_editor_junk($name)
not $self->{"include_editor_junk"} $self->_is_editor_junk($name)
Truth table100%3/3
not(A) and B
A= not $self->{"include_editor_junk"}B= $self->_is_editor_junk($name)
ABresult
0X0
100
111
382  
383 221 $directory = abs2rel($directory, $sp);
384  
385 # If we have a mixed-case package name, assume case has been preserved
386 # correctly. Otherwise, root through the file to locate the case-preserved
387 # version of the package name.
388 221 my @pkg_dirs = ();
389 221 ▶ if ( $name eq lc($name) || $name eq uc($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq lc $name or $name eq uc $name) 2 219 221
Condition66%2/3
$name eq lc $name or $name eq uc $name
l!l&&r!l&&!r
02219
MC/DC50%1/2
$name eq lc $name or $name eq uc $name
$name eq lc $name $name eq uc $name
Truth table66%2/3
A or B
A= $name eq lc $nameB= $name eq uc $name
ABresult
1X1
011
000
390 2 my $pkg_file = catfile($sp, $directory, "$name$suffix");
391 2 ▶ open PKGFILE, "<$pkg_file" or die "search_paths: Can't open $pkg_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open PKGFILE, "<$pkg_file" or die "search_paths: Can't open ${pkg_file}: $!" 2 0 2
392 2 my $in_pod = 0;
393 2 ▶ while ( my $line = <PKGFILE> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline PKGFILE)) 2 0 2
394 2 ▶ $in_pod = 1 if $line =~ m/^=\w/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^=\w/ 0 2 2
395 2 ▶ $in_pod = 0 if $line =~ /^=cut/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^=cut/ 0 2 2
396 2 ▶ next if ($in_pod || $line =~ /^=cut/); # skip pod text
Branch: 1 branch
BranchTrueFalseTotal
if $in_pod or $line =~ /^=cut/ 0 2 2
Condition33%1/3
$in_pod or $line =~ /^=cut/
l!l&&r!l&&!r
002
MC/DC0%0/2
$in_pod or $line =~ /^=cut/
$in_pod $line =~ /^=cut/
Truth table33%1/3
A or B
A= $in_podB= $line =~ /^=cut/
ABresult
1X1
011
000
397 2 ▶ next if $line =~ /^\s*#/; # and comments
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*#/ 0 2 2
398 2 ▶ if ( $line =~ m/^\s*package\s+(.*::)?($name)\s*;/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*package\s+(.*::)?($name)\s*;/i) 2 0 2
399 2 ▶ @pkg_dirs = split /::/, $1 if defined $1;;
Branch: 1 branch
BranchTrueFalseTotal
if defined $1 2 0 2
400 2 $name = $2;
401 2 last;
402 }
403 }
404 2 close PKGFILE;
405 }
406  
407 # then create the class name in a cross platform way
408 221 ▶ $directory =~ s/^[a-z]://i if($^O =~ /MSWin32|dos/); # remove volume
Branch: 1 branch
BranchTrueFalseTotal
if $^O =~ /MSWin32|dos/ 0 221 221
409 221 my @dirs = ();
410 221 ▶ if ($directory) {
Branch: 1 branch
BranchTrueFalseTotal
if ($directory) { } 221 0 221
411 221 ($directory) = ($directory =~ /(.*)/);
412 221 ▶ @dirs = grep(length($_), splitdir($directory))
Branch: 1 branch
BranchTrueFalseTotal
unless $directory eq "." 55 166 221
413 unless $directory eq curdir();
414 221 for my $d (reverse @dirs) {
415 55 my $pkg_dir = pop @pkg_dirs;
416 55 ▶ last unless defined $pkg_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pkg_dir 55 0 55
417 0 $d =~ s/\Q$pkg_dir\E/$pkg_dir/i; # Correct case
418 }
419 } else {
420 0 $directory = "";
421 }
422 221 my $plugin = join '::', $searchpath, @dirs, $name;
423  
424 221 ▶ next unless $plugin =~ m!(?:[a-z\d]+)[a-z\d]*!i;
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin =~ /(?:[a-z\d]+)[a-z\d]*/i 0 221 221
425  
426 221 $self->handle_finding_plugin($plugin, \@plugins)
427 }
428  
429 # now add stuff that may have been in package
430 # NOTE we should probably use all the stuff we've been given already
431 # but then we can't unload it :(
432 101 push @plugins, $self->handle_innerpackages($searchpath);
433 } # foreach $searchpath
434  
435 694 return @plugins;
436 }
437  
438 219 ▶sub _is_editor_junk {
Subroutine _is_editor_junk: called CC=4 SCAR=14.0
439 219 my $self = shift;
440 219 my $name = shift;
441  
442 # Emacs (and other Unix-y editors) leave temp files ending in a
443 # tilde as a backup.
444 219 ▶ return 1 if $name =~ /~$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /~$/ 0 219 219
445 # Emacs makes these files while a buffer is edited but not yet
446 # saved.
447 219 ▶ return 1 if $name =~ /^\.#/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^\.#/ 1 218 219
448 # Vim can leave these files behind if it crashes.
449 218 ▶ return 1 if $name =~ /^[._].*\.s[a-w][a-z]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^[._].*\.s[a-w][a-z]$/ 0 218 218
450  
451 218 return 0;
452 }
453  
454 =head2 handle_finding_plugin($plugin, \@plugins, $no_require)
455  
456 $finder->handle_finding_plugin('MyApp::Plugin::Foo', \@plugins);
457  
458 Called for each candidate package name discovered during the search. Applies
459 the C<only>/C<except>/C<min_depth>/C<max_depth> filters, invokes
460 C<before_require> and C<after_require> triggers, optionally C<require>s the
461 module, and pushes it onto C<\@plugins> if everything succeeds.
462  
463 Pass a true value for C<$no_require> to skip the C<require> step (used when
464 the module is known to already be loaded, e.g. for inner packages).
465  
466 =cut
467  
468 278 ▶sub handle_finding_plugin {
Subroutine handle_finding_plugin: called CC=13 SCAR=25.8
Pod documented
469 278 my $self = shift;
470 278 my $plugin = shift;
471 278 my $plugins = shift;
472 278 ▶ my $no_req = shift || 0;
Condition100%2/2
shift() || 0
l!l
56222
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
473  
474 278 ▶ return unless $self->_is_legit($plugin);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_legit($plugin) 52 226 278
475 226 ▶ unless (defined $self->{'instantiate'} || $self->{'require'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"instantiate"} or $self->{"require"}) 161 65 226
Condition100%3/3
defined $self->{"instantiate"} or $self->{"require"}
l!l&&r!l&&!r
956161
MC/DC100%2/2
defined $self->{"instantiate"} or $self->{"require"}
defined $self->{"instantiate"} $self->{"require"}
Truth table100%3/3
A or B
A= defined $self->{"instantiate"}B= $self->{"require"}
ABresult
1X1
011
000
476 161 push @$plugins, $plugin;
477 161 return;
478 }
479  
480 65 ▶ $self->{before_require}->($plugin) || return if defined $self->{before_require};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"before_require"} 11 54 65
Condition80%4/5
defined $self->{'before_require'} && ($self->{'before_require'}->($plugin) || (return))
!ll&&!rl&&r
54110
$self->{"before_require"}->($plugin) or return
l!l
101
MC/DC0%0/2
defined $self->{'before_require'} && ($self->{'before_require'}->($plugin) || (return))
defined $self->{'before_require'} $self->{"before_require"}->($plugin)
Truth table66%2/3
A && B
A= defined $self->{'before_require'}B= $self->{"before_require"}->($plugin)
ABresult
0X0
100
111
481 64 ▶ unless ($no_req) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_req) 20 44 64
482 20 my $tmp = $@;
483 20 my $res = eval { require_module($plugin) };
484 20 my $err = $@;
485 20 $@ = $tmp;
486 20 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 3 17 20
487 3 ▶ if (defined $self->{on_require_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'on_require_error'}) { } 3 0 3
488 3 ▶ $self->{on_require_error}->($plugin, $err) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"on_require_error"}->($plugin, $err) or return 0 2 2
489 } else {
490 0 return;
491 }
492 }
493 }
494 61 ▶ $self->{after_require}->($plugin) || return if defined $self->{after_require};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"after_require"} 12 49 61
Condition80%4/5
defined $self->{'after_require'} && ($self->{'after_require'}->($plugin) || (return))
!ll&&!rl&&r
49120
$self->{"after_require"}->($plugin) or return
l!l
93
MC/DC0%0/2
defined $self->{'after_require'} && ($self->{'after_require'}->($plugin) || (return))
defined $self->{'after_require'} $self->{"after_require"}->($plugin)
Truth table66%2/3
A && B
A= defined $self->{'after_require'}B= $self->{"after_require"}->($plugin)
ABresult
0X0
100
111
495 58 push @$plugins, $plugin;
496 }
497  
498 =head2 find_files($search_path)
499  
500 my @files = $finder->find_files($dir);
501  
502 Recursively finds all files matching C<file_regex> (default C<qr/\.pm$/>) under
503 C<$dir> using L<File::Find>. Respects the C<follow_symlinks> option.
504  
505 =cut
506  
507 103 ▶sub find_files {
Subroutine find_files: called CC=2 SCAR=6.9
Pod documented
508 103 my $self = shift;
509 103 my $search_path = shift;
510 103 ▶ my $file_regex = $self->{'file_regex'} || qr/\.pm$/;
Condition100%2/2
$self->{'file_regex'} || qr/\.pm$/
l!l
2101
MC/DC100%1/1
$self->{'file_regex'} || qr/\.pm$/
$self->{'file_regex'}
Truth table100%2/2
A
A= $self->{'file_regex'}
Aresult
11
00
511  
512  
513 # find all the .pm files in it
514 # this isn't perfect and won't find multiple plugins per file
515 #my $cwd = Cwd::getcwd;
516 103 my @files = ();
517 { # for the benefit of perl 5.6.1's Find, localize topic
518 103 local $_;
519 File::Find::find( { no_chdir => 1,
520 follow => $self->{'follow_symlinks'},
521 391 ▶ wanted => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
522 # Inlined from File::Find::Rule C< name => '*.pm' >
523 391 ▶ return unless $File::Find::name =~ /$file_regex/;
Branch: 1 branch
BranchTrueFalseTotal
unless $File::Find::name =~ /$file_regex/ 166 225 391
524 225 (my $path = $File::Find::name) =~ s#^\\./##;
525 225 push @files, $path;
526 }
527 103 }, $search_path );
528 }
529 #chdir $cwd;
530 103 return @files;
531  
532 }
533  
534 =head2 handle_inc_hooks($search_path, @dirs)
535  
536 my @plugins = $finder->handle_inc_hooks($path, @SEARCHDIR);
537  
538 Handles the case where an entry in C<@INC> is a blessed object with a
539 C<files()> method (as used by L<App::FatPacker> and L<PAR>). Calls
540 C<files()> on each such object, filters by C<$search_path>, and delegates
541 to L</handle_finding_plugin>.
542  
543 =cut
544  
545 88 ▶sub handle_inc_hooks {
Subroutine handle_inc_hooks: called CC=6 SCAR=18.0
Pod documented
546 88 my $self = shift;
547 88 my $path = shift;
548 88 my @SEARCHDIR = @_;
549  
550 88 my @plugins;
551 88 for my $dir ( @SEARCHDIR ) {
552 703 ▶ next unless blessed( $dir ) && $dir->can( 'files' );
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dir) and $dir->can("files") 702 1 703
Condition66%2/3
builtin::blessed($dir) and $dir->can("files")
!ll&&!rl&&r
70201
MC/DC50%1/2
builtin::blessed($dir) and $dir->can("files")
builtin::blessed($dir) $dir->can("files")
Truth table66%2/3
A and B
A= builtin::blessed($dir)B= $dir->can("files")
ABresult
0X0
100
111
553  
554 1 foreach my $plugin ( $dir->files ) {
555 1 $plugin =~ s/\.pm$//;
556 1 $plugin =~ s{/}{::}g;
557 1 ▶ next unless $plugin =~ m!^${path}::!;
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin =~ /^$path::/ 0 1 1
558 1 $self->handle_finding_plugin( $plugin, \@plugins );
559 }
560 }
561 88 return @plugins;
562 }
563  
564 =head2 handle_innerpackages($search_path)
565  
566 my @plugins = $finder->handle_innerpackages('MyApp::Plugin');
567  
568 Uses L<Devel::InnerPackage> to find any package declared inside an already-
569 loaded file whose name falls under C<$search_path>. This is how
570 C<Something::Plugin::Bar> is discovered when it is defined inside
571 F<Something/Plugin/Foo.pm>. Set C<< inner => 0 >> to disable this behaviour.
572  
573 =cut
574  
575 189 ▶sub handle_innerpackages {
Subroutine handle_innerpackages: called CC=4 SCAR=13.9
Pod documented
576 189 my $self = shift;
577 189 ▶ return () if (exists $self->{inner} && !$self->{inner});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"inner"} and not $self->{"inner"} 3 186 189
Condition100%3/3
exists $self->{"inner"} and not $self->{"inner"}
!ll&&!rl&&r
18153
MC/DC100%2/2
exists $self->{"inner"} and not $self->{"inner"}
exists $self->{"inner"} not $self->{"inner"}
Truth table100%3/3
A and not(B)
A= exists $self->{"inner"}B= not $self->{"inner"}
ABresult
0X0
100
111
578  
579 186 my $path = shift;
580 186 my @plugins;
581  
582 186 foreach my $plugin (Devel::InnerPackage::list_packages($path)) {
583 56 $self->handle_finding_plugin($plugin, \@plugins, 1);
584 }
585 186 return @plugins;
586  
587 }
588  
589 43 1;
590  
591 =head1 OPTIONS
592  
593 All options are passed to C<new> as a flat hash and may be set or overridden
594 directly on the object before calling C<plugins>.
595  
596 =head2 package
597  
598 The package name used to build the default C<search_path>. When using
599 L<Module::Pluggable> this is set automatically to the calling package. When
600 using C<Module::Pluggable::Object> directly you should set it explicitly.
601  
602 =head2 search_path
603  
604 An array ref (or a single string, which is promoted to an array ref) of
605 namespace prefixes to search. Defaults to C<["${package}::Plugin"]>.
606  
607 search_path => ['MyApp::Plugin', 'MyApp::Extension']
608  
609 Alternatively, you can pass a hash ref where each key is a namespace prefix and
610 each value is a hash ref of option overrides for that path. The per-path
611 options are merged on top of the global options, so you only need to
612 specify what differs.
613  
614 search_path => {
615 'MyApp::Plugin' => { max_depth => 2 },
616 'MyApp::Extension' => { max_depth => 3, instantiate => 'new' },
617 }
618  
619 As another alternative, if you want plugins to be in a particular order, use
620 an array ref of mixed plain strings and single-key hash refs. Order is
621 preserved; only the hash ref entries carry per-path option overrides.
622  
623 search_path => [
624 'MyApp::Plugin',
625 { 'MyApp::Extension' => { max_depth => 3, instantiate => 'new' } },
626 ]
627  
628 Results from all paths are combined and returned in a single list. Class
629 names are deduplicated across paths; if any path uses C<instantiate> its
630 objects are included as-is alongside any class names from other paths.
631  
632 =head2 search_dirs
633  
634 An array ref (or a single string) of filesystem directories to search
635 I<before> C<@INC>. Use this when plugins live outside the normal include
636 path.
637  
638 search_dirs => ['/opt/myapp/plugins']
639  
640 =head2 search_dirs_strict
641  
642 When true, only the directories in C<search_dirs> are searched; C<@INC> is
643 ignored entirely. Requires C<search_dirs> to also be set.
644  
645 search_dirs_strict => 1
646  
647 =head2 instantiate
648  
649 Name of the method to call on each plugin class to create an instance.
650 Typically C<'new'>. When set, C<plugins()> returns a list of objects instead
651 of class names. Arguments passed to C<plugins()> are forwarded to this method.
652  
653 instantiate => 'new'
654  
655 =head2 require
656  
657 When true, each plugin module is C<require>d but not instantiated. Overrides
658 C<instantiate>. Also enables inner-package discovery (see L</inner>).
659  
660 require => 1
661  
662 =head2 inner
663  
664 Controls whether inner packages (secondary C<package> declarations inside a
665 single F<.pm> file) are discovered.
666  
667 =over 4
668  
669 =item * Defaults to true when C<require> or C<instantiate> is set.
670  
671 =item * Set to C<0> to disable even when C<require> is set.
672  
673 =back
674  
675 inner => 0
676  
677 =head2 only
678  
679 Restricts the returned plugins to those matching this value. May be:
680  
681 =over 4
682  
683 =item * A string - only the exact named plugin is returned.
684  
685 =item * An array ref of strings - only those exact plugins are returned.
686  
687 =item * A compiled regex - only plugins whose name matches are returned.
688  
689 =back
690  
691 only => qr/^MyApp::Plugin::Safe/
692  
693 =head2 except
694  
695 Excludes matching plugins from the results. Accepts the same forms as C<only>.
696  
697 except => ['MyApp::Plugin::Broken', 'MyApp::Plugin::Deprecated']
698  
699 =head2 min_depth / max_depth
700  
701 Restrict discovery to plugins at a certain namespace depth (number of
702 C<::>-separated components).
703  
704 For example, C<MyApp::Plugin::Foo> has depth 3 and
705 C<MyApp::Plugin::Foo::Bar> has depth 4.
706  
707 max_depth => 3 # only MyApp::Plugin::Foo, not MyApp::Plugin::Foo::Bar
708 min_depth => 4 # only MyApp::Plugin::Foo::Bar and deeper
709  
710 =head2 file_regex
711  
712 A compiled regex used to identify plugin files. Defaults to C<qr/\.pm$/>.
713  
714 file_regex => qr/\.plugin$/
715  
716 =head2 follow_symlinks
717  
718 Whether to follow symbolic links when walking the filesystem. Defaults to
719 C<1> (follow symlinks) on all platforms except Windows with
720 L<File::Find> E<ge> 1.39, where it defaults to C<0> to preserve the
721 historical behaviour.
722  
723 follow_symlinks => 0
724  
725 =head2 include_editor_junk
726  
727 By default, files that look like editor artefacts are silently skipped:
728  
729 =over 4
730  
731 =item * Files ending in C<~> (Emacs / generic backup files).
732  
733 =item * Files beginning with C<.#> (Emacs lock files).
734  
735 =item * Files matching C<^[._].*\.s[a-w][a-z]$> (Vim swap files).
736  
737 =back
738  
739 Set C<include_editor_junk> to a true value to disable this filtering and
740 include every file that matches C<file_regex>.
741  
742 include_editor_junk => 1
743  
744 =head2 force_search_all_paths
745  
746 By default, when the module is loaded from inside a C<blib/> tree (i.e.
747 during C<make test>), only C<@INC> entries that themselves contain C<blib>
748 are searched. Set this option to search all of C<@INC> regardless.
749  
750 force_search_all_paths => 1
751  
752 =head2 sort_results
753  
754 Controls the order plugin names (or objects) are returned in. One of:
755  
756 =over 4
757  
758 =item * C<'alpha'> (the default) - sorted alphabetically by package name.
759  
760 =item * C<'path'> - returned in search-path discovery order: all plugins
761 under the first C<search_path> entry, then the second, and so on. Within a
762 single path, order follows the underlying filesystem traversal.
763  
764 =item * C<0> - no sort is applied at all. Fastest option, for callers that
765 will sort or otherwise order the results themselves.
766  
767 =back
768  
769 sort_results => 'path'
770  
771 =head1 TRIGGERS
772  
773 Triggers are callbacks passed as options to C<new>. If a trigger returns
774 C<0> (or any false value), the plugin is dropped at that point in the
775 pipeline.
776  
777 =head2 before_require
778  
779 before_require => sub { my ($plugin) = @_; ... }
780  
781 Called with the plugin package name before it is C<require>d. Return C<0> to
782 skip both the C<require> and all subsequent steps for this plugin.
783  
784 =head2 on_require_error
785  
786 on_require_error => sub { my ($plugin, $err) = @_; ... }
787  
788 Called when C<require> raises an exception. The default handler C<carp>s the
789 error and returns C<0> (dropping the plugin). Return a true value to keep the
790 plugin in the list despite the error.
791  
792 =head2 after_require
793  
794 after_require => sub { my ($plugin) = @_; ... }
795  
796 Called after a successful C<require>. Return C<0> to exclude the plugin from
797 results even though it has already been loaded.
798  
799 =head2 before_instantiate
800  
801 before_instantiate => sub { my ($plugin) = @_; ... }
802  
803 Called with the plugin package name before the C<instantiate> method is
804 invoked. Return C<0> to skip instantiation for this plugin.
805  
806 =head2 on_instantiate_error
807  
808 on_instantiate_error => sub { my ($plugin, $err) = @_; ... }
809  
810 Called when the C<instantiate> method raises an exception. The default handler
811 C<carp>s the error and returns C<0>.
812  
813 =head2 after_instantiate
814  
815 after_instantiate => sub { my ($plugin, $obj) = @_; return $obj }
816  
817 Called after successful instantiation with the plugin name and the newly
818 created object. The return value is used as the plugin entry in the results
819 list; return a false value to discard the plugin entirely. Use this to wrap
820 or decorate plugin objects transparently.
821  
822 =head1 BEHAVIOUR UNDER TEST ENVIRONMENTS
823  
824 When C<blib.pm> is present in C<%INC> and the calling file's path contains
825 C<blib/>, only C<@INC> entries that themselves contain C<blib> are searched.
826 This prevents installed (non-blib) versions of plugins from interfering with
827 tests. See C<force_search_all_paths> to override this.
828  
829 =head1 @INC HOOKS AND App::FatPacker
830  
831 If an entry in C<@INC> is a blessed object with a C<files()> method, its
832 C<files()> list is searched for plugin modules. This is how L<App::FatPacker>
833 (from version 0.10.0 onwards) integrates with C<Module::Pluggable::Object>,
834 and how L<PAR> can be made to do the same. See F<t/26inc_hook.t> for a
835 working example.
836  
837 =head1 AUTHOR
838  
839 Simon Wistow <simon@thegestalt.org>
840  
841 =head1 COPYING
842  
843 Copyright, 2006-2026 Simon Wistow
844  
845 Distributed under the same terms as Perl itself.
846  
847 =head1 SEE ALSO
848  
849 L<Module::Pluggable>, L<Devel::InnerPackage>,
850 L<Module::Runtime>, L<App::FatPacker>, L<PAR>
851  
852 =cut
853  
↑ Summary Module/Pluggable.pm »