File Coverage

blib/lib/Module/Find.pm
Criterion Covered Total %
statement 69 69 100.0
branch 22 28 78.5
condition 7 9 77.7
subroutine 14 14 100.0
pod 7 7 100.0
total 119 127 93.7


line stmt bran cond sub pod time code
1             package Module::Find;
2              
3 12     12   633917 use 5.006001;
  12         112  
4 12     12   60 use strict;
  12         18  
  12         302  
5 12     12   61 use warnings;
  12         18  
  12         347  
6              
7 12     12   56 use File::Spec;
  12         16  
  12         327  
8 12     12   71 use File::Find;
  12         25  
  12         12881  
9              
10             our $VERSION = '0.15';
11              
12             our $basedir = undef;
13             our @results = ();
14             our $prune = 0;
15             our $followMode = 1;
16              
17             our @ISA = qw(Exporter);
18              
19             our @EXPORT = qw(findsubmod findallmod usesub useall setmoduledirs);
20              
21             our @EXPORT_OK = qw(followsymlinks ignoresymlinks);
22              
23             =encoding utf-8
24              
25             =head1 NAME
26              
27             Module::Find - Find and use installed modules in a (sub)category
28              
29             =head1 SYNOPSIS
30              
31             use Module::Find;
32              
33             # use all modules in the Plugins/ directory
34             @found = usesub Mysoft::Plugins;
35              
36             # use modules in all subdirectories
37             @found = useall Mysoft::Plugins;
38              
39             # find all DBI::... modules
40             @found = findsubmod DBI;
41              
42             # find anything in the CGI/ directory
43             @found = findallmod CGI;
44            
45             # set your own search dirs (uses @INC otherwise)
46             setmoduledirs(@INC, @plugindirs, $appdir);
47            
48             # not exported by default
49             use Module::Find qw(ignoresymlinks followsymlinks);
50            
51             # ignore symlinks
52             ignoresymlinks();
53            
54             # follow symlinks (default)
55             followsymlinks();
56              
57             =head1 DESCRIPTION
58              
59             Module::Find lets you find and use modules in categories. This can be very
60             useful for auto-detecting driver or plugin modules. You can differentiate
61             between looking in the category itself or in all subcategories.
62              
63             If you want Module::Find to search in a certain directory on your
64             harddisk (such as the plugins directory of your software installation),
65             make sure you modify C<@INC> before you call the Module::Find functions.
66              
67             =head1 FUNCTIONS
68              
69             =over
70              
71             =item C
72              
73             Sets the directories to be searched for modules. If not set, Module::Find
74             will use @INC. If you use this function, @INC will I be included
75             automatically, so add it if you want it. Set to undef to revert to
76             default behaviour.
77              
78             =cut
79              
80             sub setmoduledirs {
81 6     6 1 2231 return @Module::Find::ModuleDirs = grep { defined } @_;
  5         24  
82             }
83              
84             =item C<@found = findsubmod Module::Category>
85              
86             Returns modules found in the Module/Category subdirectories of your perl
87             installation. E.g. C will return C, but
88             not C .
89              
90             =cut
91              
92             sub findsubmod(*) {
93 9     9 1 1674 $prune = 1;
94            
95 9         25 return _find($_[0]);
96             }
97              
98             =item C<@found = findallmod Module::Category>
99              
100             Returns modules found in the Module/Category subdirectories of your perl
101             installation. E.g. C will return C and also
102             C .
103              
104             =cut
105              
106             sub findallmod(*) {
107 10     10 1 4405 $prune = 0;
108            
109 10         28 return _find($_[0]);
110             }
111              
112             =item C<@found = usesub Module::Category>
113              
114             Uses and returns modules found in the Module/Category subdirectories of your perl
115             installation. E.g. C will return C, but
116             not C .
117              
118             If any module dies during loading, usesub will also die at this point.
119              
120             =cut
121              
122             sub usesub(*) {
123 3     3 1 86 $prune = 1;
124            
125 3         11 my @r = _find($_[0]);
126              
127 3 100       11 local @INC = @Module::Find::ModuleDirs
128             if (@Module::Find::ModuleDirs);
129            
130 3         7 foreach my $m (@r) {
131 3         232 eval " require $m; import $m ; ";
132 3 50       18 die $@ if $@;
133             }
134            
135 3         11 return @r;
136             }
137              
138             =item C<@found = useall Module::Category>
139              
140             Uses and returns modules found in the Module/Category subdirectories of your perl installation. E.g. C will return C and also
141             C .
142              
143             If any module dies during loading, useall will also die at this point.
144              
145             =cut
146              
147             sub useall(*) {
148 5     5 1 904 $prune = 0;
149            
150 5         18 my @r = _find($_[0]);
151            
152 5 100       27 local @INC = @Module::Find::ModuleDirs
153             if (@Module::Find::ModuleDirs);
154            
155 5         11 foreach my $m (@r) {
156 9         567 eval " require $m; import $m; ";
157 9 50       43 die $@ if $@;
158             }
159            
160 5         20 return @r;
161             }
162              
163             # 'wanted' functions for find()
164             # you know, this would be a nice application for currying...
165             sub _wanted {
166 71     71   4219 my $name = File::Spec->abs2rel($_, $basedir);
167 71 100 66     2433 return unless $name && $name ne File::Spec->curdir() && substr($name, 0, 1) ne '.';
      66        
168              
169 49 100 100     789 if (-d && $prune) {
170 9         22 $File::Find::prune = 1;
171 9         131 return;
172             }
173              
174 40 100       875 return unless /\.pm$/;
175              
176 30         119 $name =~ s|\.pm$||;
177 30         152 $name = join('::', File::Spec->splitdir($name));
178              
179 30         1176 push @results, $name;
180             }
181              
182              
183             # helper functions for finding files
184              
185             sub _find(*) {
186 27     27   55 my ($category) = @_;
187 27 50       73 return undef unless defined $category;
188              
189 27         309 my $dir = File::Spec->catdir(split(/::|'/, $category));
190              
191 27         72 @results = ();
192              
193 27 100       102 foreach my $inc (@Module::Find::ModuleDirs ? @Module::Find::ModuleDirs : @INC) {
194 211 100       439 if (ref $inc) {
195 1 50       2 if (my @files = eval { $inc->files }) {
  1         5  
196             push @results,
197 1 50       16 map { s/^\Q$category\E::// ? $_ : () }
198 1         4 map { s{/}{::}g; s{\.pm$}{}; $_ }
  1         13  
  1         3  
199 1         10 grep { /\.pm$/ }
  1         5  
200             @files;
201             }
202             }
203             else {
204 210         1023 our $basedir = File::Spec->catdir($inc, $dir);
205              
206 210 100       2496 next unless -d $basedir;
207 22         2565 find({wanted => \&_wanted,
208             no_chdir => 1,
209             follow => $followMode}, $basedir);
210             }
211             }
212              
213             # filter duplicate modules
214 27         69 my %seen = ();
215 27         102 @results = grep { not $seen{$_}++ } @results;
  31         127  
216              
217 27         109 @results = map "$category\::$_", @results;
218              
219             @results = map {
220 27 50       74 ($_ =~ m{^(\w+(?:(?:::|')\w+)*)$})[0] || die "$_ does not look like a package name"
  29         218  
221             } @results;
222              
223 27         239 return @results;
224             }
225              
226             =item C
227              
228             Do not follow symlinks. This function is not exported by default.
229              
230             =cut
231              
232             sub ignoresymlinks {
233 1     1 1 758 $followMode = 0;
234             }
235              
236             =item C
237              
238             Follow symlinks (default behaviour). This function is not exported by default.
239              
240             =cut
241              
242             sub followsymlinks {
243 1     1 1 238 $followMode = 1;
244             }
245              
246             =back
247              
248             =head1 HISTORY
249              
250             =over 8
251              
252             =item 0.01, 2004-04-22
253              
254             Original version; created by h2xs 1.22
255              
256             =item 0.02, 2004-05-25
257              
258             Added test modules that were left out in the first version. Thanks to
259             Stuart Johnston for alerting me to this.
260              
261             =item 0.03, 2004-06-18
262              
263             Fixed a bug (non-localized $_) by declaring a loop variable in use functions.
264             Thanks to Stuart Johnston for alerting me to this and providing a fix.
265              
266             Fixed non-platform compatibility by using File::Spec.
267             Thanks to brian d foy.
268              
269             Added setmoduledirs and updated tests. Idea shamelessly stolen from
270             ...errm... inspired by brian d foy.
271              
272             =item 0.04, 2005-05-20
273              
274             Added POD tests.
275              
276             =item 0.05, 2005-11-30
277              
278             Fixed issue with bugfix in PathTools-3.14.
279              
280             =item 0.06, 2008-01-26
281              
282             Module::Find now won't report duplicate modules several times anymore (thanks to Uwe Völker for the report and the patch)
283              
284             =item 0.07, 2009-09-08
285              
286             Fixed RT#38302: Module::Find now follows symlinks by default (can be disabled).
287              
288             =item 0.08, 2009-09-08
289              
290             Fixed RT#49511: Removed Mac OS X extended attributes from distribution
291              
292             =item 0.09, 2010-02-26
293              
294             Fixed RT#38302: Fixed META.yml generation (thanks very much to cpanservice for the help).
295              
296             =item 0.10, 2010-02-26
297              
298             Fixed RT#55010: Removed Unicode BOM from Find.pm.
299              
300             =item 0.11, 2012-05-22
301              
302             Fixed RT#74251: defined(@array) is deprecated under Perl 5.15.7.
303             Thanks to Roman F, who contributed the implementation.
304              
305             =item 0.12, 2014-02-08
306              
307             Fixed RT#81077: useall fails in taint mode
308             Thanks to Aran Deltac, who contributed the implementation and test.
309              
310             Fixed RT#83596: Documentation doesn't describe behaviour if a module fails to load
311             Clarified documentation for useall and usesub.
312              
313             Fixed RT#62923: setmoduledirs(undef) doesn't reset to searching @INC
314             Added more explicit tests.
315             Thanks to Colin Robertson for his input.
316              
317             =item 0.13, 2015-03-09
318              
319             This release contains two contributions from Moritz Lenz:
320              
321             Link to Module::Pluggable and Class::Factory::Util in "SEE ALSO"
322              
323             Align package name parsing with how perl does it (allowing single quotes as module separator)
324              
325             Also, added a test for meta.yml
326              
327             =item 0.14, 2019-12-25
328              
329             A long overdue update. Thank you for the many contributions!
330              
331             Fixed RT#99055: Removed file readability check (pull request contributed by Moritz Lenz)
332              
333             Now supports @INC hooks (pull request contributed by Graham Knop)
334              
335             Now filters out filenames starting with a dot (pull request contributed by Desmond Daignault)
336              
337             Now uses strict (pull request contributed by Shlomi Fish)
338              
339             Fixed RT#122016: test/ files show up in metacpan (bug report contributed by Karen Etheridge)
340              
341             =item 0.15, 2019-12-26
342              
343             Fixed RT#127657 (bug report contributed by Karen Etheridge): Module::Find now uses @ModuleDirs
344             (if specified) for loading modules. Previously, when using setmoduledirs() to set an array of
345             directories that did not contain @INC, Module::Find would find the modules correctly, but load
346             them from @INC.
347              
348             =back
349              
350             =head1 DEVELOPMENT NOTES
351              
352             Please report any bugs using the CPAN RT system. The development repository for this module is hosted on GitHub: L.
353              
354             =head1 SEE ALSO
355              
356             L, L, L
357              
358             =head1 AUTHOR
359              
360             Christian Renz, Ecrenz@web42.comE
361              
362             =head1 COPYRIGHT AND LICENSE
363              
364             Copyright 2004-2019 by Christian Renz . All rights reserved.
365              
366             This library is free software; you can redistribute it and/or modify
367             it under the same terms as Perl itself.
368              
369             =cut
370              
371             1;