blib/lib/Module/Loader.pm

filter: Statementstmt 100% 48 / 48 Branchbran 100% 10 / 10 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 5 / 5 totaltotal 100% 81 / 81 CC 11 SCAR 14.0
CC
11
Cov
100%
find_modules
8.0
max_depth
3.0
load
2.0
CRAP 4.0
↑ Summary
1 package Module::Loader;
2 5 $Module::Loader::VERSION = '0.05';
3 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Path::Iterator::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use File::Spec::Functions qw/ catfile splitdir catdir /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub new
11 5 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
12 5 my ($class, %attributes) = @_;
13  
14 5 bless {%attributes}, $class;
15 }
16  
17 sub max_depth
18 4 ▶{
Subroutine max_depth: called CC=3 SCAR=11.0
Pod documented
19 4 my $self = shift;
20  
21 4 ▶ croak 'max_depth is immutable' if @_ > 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 0 1 3 4
22 3 ▶ return $self->{max_depth} if exists($self->{max_depth});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"max_depth"} 2 1 3
23 1 return;
24 }
25  
26 sub find_modules
27 7 ▶{
Subroutine find_modules: called CC=8 SCAR=20.8
Pod documented
28 7 my $self = shift;
29 7 my $base = shift;
30 7 ▶ my $argref = @_ > 0 ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 ? : 3 4 7
31 7 ▶ my $max_depth = $argref->{max_depth} || $self->{max_depth} || 0;
Condition100%5/5
$argref->{'max_depth'} || $self->{'max_depth'} || 0
l!l
43
$argref->{"max_depth"} or $self->{"max_depth"}
l!l&&r!l&&!r
313
MC/DC100%2/2
$argref->{'max_depth'} || $self->{'max_depth'} || 0
$argref->{"max_depth"} $self->{"max_depth"}
Truth table100%3/3
A or B
A= $argref->{"max_depth"}B= $self->{"max_depth"}
ABresult
1X1
011
000
32 7 my @baseparts = split(/::/, $base);
33 7 my %modules;
34  
35 7 foreach my $inc_directory (@INC) {
36 64 my $directory = catdir($inc_directory);
37 64 my $path = catfile($directory, @baseparts);
38 64 ▶ next unless -d $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path 56 8 64
39  
40 8 my $rule = Path::Iterator::Rule->new->perl_module;
41 8 ▶ $rule->max_depth($max_depth) if $max_depth;
Branch: 1 branch
BranchTrueFalseTotal
if $max_depth 4 4 8
42  
43 8 foreach my $file ($rule->all($path)) {
44 21 (my $modpath = $file) =~ s!^\Q$directory\E.|\.pm$!!g;
45  
46 21 my $module = join('::', splitdir($modpath));
47  
48 # Using a hash means that even if a module is installed
49 # in more than one place, it will only be reported once
50 21 $modules{ $module }++;
51 }
52 }
53  
54 7 return keys(%modules);
55 }
56  
57 sub search
58 2 ▶{
Subroutine search: called CC=1 SCAR=0.0
Pod documented
59 2 my ($self, $base) = @_;
60  
61 2 return $self->find_modules($base, { max_depth => 1 });
62 }
63  
64 sub load
65 2 ▶{
Subroutine load: called CC=2 SCAR=6.9
Pod documented
66 2 my ($self, @modules) = @_;
67  
68 2 require Module::Runtime;
69 2 foreach my $module (@modules) {
70 2 Module::Runtime::require_module($module);
71 }
72 }
73  
74 5 1;
75  
76 =encoding utf8
77  
78 =head1 NAME
79  
80 Module::Loader - finding and loading modules in a given namespace
81  
82 =head1 SYNOPSIS
83  
84 use Module::Loader;
85  
86 my $loader = Module::Loader->new;
87 my @plugins = $loader->find_modules('MyApp::Plugin');
88  
89 foreach my $plugin (@plugins) {
90 $loader->load($plugin);
91 }
92  
93 =head1 DESCRIPTION
94  
95 This module provides methods for finding modules in a given namespace,
96 and then loading them. It is intended for use in situations where
97 you're looking for plugins, and then loading one or more of them.
98  
99 This module was inspired by L<Mojo::Loader>, which I have used in
100 a number of projects. But some people were wary of requiring L<Mojolicious>
101 just to get a module loader, which prompted me to create C<Module::Loader>.
102  
103 Note: this module was initially called C<Plugin::Loader>, but I realised
104 that C<Module::Loader> was a more appropriate name.
105  
106 =head2 new
107  
108 When instantiating C<Module::Loader>, you can optionally set the
109 C<max_depth> attribute, which limits the search depth when looking for modules.
110  
111 my $loader = Module::Loader->new(max_depth => 1);
112  
113 Let's say you have all of the CPAN plugins for the template toolkit
114 installed locally. If you don't specify C<max_depth>, then C<find_modules('Template::Plugin')>
115 would return L<Template::Plugin::Filter::Minify::JavaScript>
116 as well as L<Template::Plugin::File>. If you set C<max_depth> to 1,
117 then you'd get the latter but not the former.
118  
119 Why might you want to do that?
120  
121 You might have a convention where plugins are the modules immediately
122 within the specified namespace, but that each plugin can have additional
123 modules within its own namespace.
124  
125 So typically you'll either not set C<max_depth>, or you'll set it to 1.
126  
127 =head1 METHODS
128  
129 =head2 find_modules
130  
131 Takes a namespace, and returns all installed modules in that namespace,
132 that were found in C<@INC>. For example:
133  
134 @plugins = $loader->find_modules('Template::Plugin');
135  
136 By default this will find all modules in the given namespace,
137 unless you've specified a maximum search depth, as described above.
138 You can also specify C<max_depth> when you call the method:
139  
140 @plugins = $loader->find_modules('Template::Plugin',
141 { max_depth => 1 });
142  
143 =head2 search
144  
145 This is the same as C<find_modules()> above,
146 but it hard-codes the search depth to 1.
147 This method is provided for compatibility with L<Mojo::Loader>:
148  
149 @plugins = $loader->search('Template::Plugin');
150  
151 It just calls C<find_modules()> with C<max_depth> set to 1,
152 as shown above.
153  
154 =head2 load
155  
156 Takes a module name and tries to load the module.
157  
158 $loader->load('MyModule::Plugin::Spank');
159  
160 If loading fails, then we C<croak>.
161  
162 =head2 max_depth
163  
164 Returns the value of the C<max_depth>, if it was passed to the constructor,
165 otherwise C<undef>.
166  
167 =head1 SEE ALSO
168  
169 L<Mojo::Loader> was the inspiration for this module, but has
170 a slightly different interface. In particular, it has C<max_depth>
171 hard-coded to 1.
172  
173 L<Module::Pluggable> is effectively a role which gives a class the ability
174 to find plugins within its namespace.
175  
176 L<Module::Pluggable::Ordered> is similar to L<Module::Pluggable>,
177 but lets you control the order in which modules are loaded.
178  
179 L<all> will load all modules in a given namespace, eg with C<use all 'IO::*';>
180  
181 L<lib::require::all> will load all modules found in a given I<directory>
182 (as opposed to a namespace).
183  
184 L<MAD::Loader> provides functions for loading modules,
185 but not for finding them.
186  
187 L<Module::Find> provides a number of functions for finding and loading
188 modules. It provides different functions depending on whether you want
189 to limit the search depth to 1 or not: C<findallmod> vs C<findsubmod>.
190  
191 L<Module::Recursive::Require> will load all modules in a given namespace,
192 and return a list of the modules found / loaded.
193 It lets you provide regexps for filtering out certain namespaces.
194  
195 L<Module::Require> provides two functions, C<require_regex>
196 and C<require_glob> which will load all locally installed modules
197 whose name matches a pattern (specified as a regular expression
198 or glob-style pattern).
199  
200 =head1 REPOSITORY
201  
202 L<https://github.com/neilbowers/Module-Loader>
203  
204 =head1 AUTHOR
205  
206 Neil Bowers E<lt>neilb@cpan.orgE<gt>
207  
208 =head1 COPYRIGHT AND LICENSE
209  
210 This software is copyright (c) 2014 by Neil Bowers <neilb@cpan.org>.
211  
212 This is free software; you can redistribute it and/or modify it under
213 the same terms as the Perl 5 programming language system itself.
214  
215 =cut
216  
↑ Summary