File Coverage

blib/lib/Module/Find.pm
Criterion Covered Total %
statement 69 69 100.0
branch 24 28 85.7
condition 7 9 77.7
subroutine 14 14 100.0
pod 7 7 100.0
total 121 127 95.2


line stmt bran cond sub pod time code
1             package Module::Find;
2              
3 13     13   684699 use 5.008001;
  13         108  
4 13     13   55 use strict;
  13         15  
  13         259  
5 13     13   64 use warnings;
  13         17  
  13         324  
6              
7 13     13   59 use File::Spec;
  13         19  
  13         291  
8 13     13   73 use File::Find;
  13         25  
  13         14268  
9              
10             our $VERSION = '0.16';
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 1783 return @Module::Find::ModuleDirs = grep { defined } @_;
  5         21  
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 10     10 1 1797 $prune = 1;
94            
95 10         28 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 3877 $prune = 0;
108            
109 10         22 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 4     4 1 1278 $prune = 1;
124            
125 4         11 my @r = _find($_[0]);
126              
127 4 100       15 local @INC = @Module::Find::ModuleDirs
128             if (@Module::Find::ModuleDirs);
129            
130 4         10 foreach my $m (@r) {
131 4         351 eval " require $m; import $m ; ";
132 4 100       44 die $@ if $@;
133             }
134            
135 3         10 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 6     6 1 2060 $prune = 0;
149            
150 6         19 my @r = _find($_[0]);
151            
152 6 100       24 local @INC = @Module::Find::ModuleDirs
153             if (@Module::Find::ModuleDirs);
154            
155 6         11 foreach my $m (@r) {
156 10         620 eval " require $m; import $m; ";
157 10 100       67 die $@ if $@;
158             }
159            
160 5         17 return @r;
161             }
162              
163             # 'wanted' functions for find()
164             # you know, this would be a nice application for currying...
165             sub _wanted {
166 77     77   4219 my $name = File::Spec->abs2rel($_, $basedir);
167 77 100 66     2604 return unless $name && $name ne File::Spec->curdir() && substr($name, 0, 1) ne '.';
      66        
168              
169 52 100 100     774 if (-d && $prune) {
170 9         25 $File::Find::prune = 1;
171 9         120 return;
172             }
173              
174 43 100       838 return unless /\.pm$/;
175              
176 33         112 $name =~ s|\.pm$||;
177 33         159 $name = join('::', File::Spec->splitdir($name));
178              
179 33         1076 push @results, $name;
180             }
181              
182              
183             # helper functions for finding files
184              
185             sub _find(*) {
186 30     30   61 my ($category) = @_;
187 30 50       72 return undef unless defined $category;
188              
189 30         306 my $dir = File::Spec->catdir(split(/::|'/, $category));
190              
191 30         62 @results = ();
192              
193 30 100       86 foreach my $inc (@Module::Find::ModuleDirs ? @Module::Find::ModuleDirs : @INC) {
194 241 100       447 if (ref $inc) {
195 1 50       2 if (my @files = eval { $inc->files }) {
  1         2  
196             push @results,
197 1 50       15 map { s/^\Q$category\E::// ? $_ : () }
198 1         3 map { s{/}{::}g; s{\.pm$}{}; $_ }
  1         12  
  1         4  
199 1         12 grep { /\.pm$/ }
  1         5  
200             @files;
201             }
202             }
203             else {
204 240         1006 our $basedir = File::Spec->catdir($inc, $dir);
205              
206 240 100       2688 next unless -d $basedir;
207 25         2662 find({wanted => \&_wanted,
208             no_chdir => 1,
209             follow => $followMode}, $basedir);
210             }
211             }
212              
213             # filter duplicate modules
214 30         73 my %seen = ();
215 30         51 @results = grep { not $seen{$_}++ } @results;
  34         156  
216              
217 30         112 @results = map "$category\::$_", @results;
218              
219             @results = map {
220 30 50       71 ($_ =~ m{^(\w+(?:(?:::|')\w+)*)$})[0] || die "$_ does not look like a package name"
  32         232  
221             } @results;
222              
223 30         206 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 690 $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 243 $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             =item 0.16, 2022-08-01
349              
350             Fixes an issue where symlink tests failed on systems that do not support creation of symlinks.
351             The issue appears on Windows systems due to changed behaviour in C described
352             in L
353             Symlink tests were previously skipped if C is not available, and now
354             also if creation of a symlink is not possible.
355              
356             Fixes L. Note that on Windows system,
357             the patch to C from L
358             will be required for proper operation.
359              
360             =back
361              
362             =head1 DEVELOPMENT NOTES
363              
364             The development repository for this module is hosted on GitHub: L. Please report any bugs by opening an issue there.
365              
366             =head1 SEE ALSO
367              
368             L, L, L
369              
370             =head1 AUTHOR
371              
372             Christian Renz, Ecrenz@web42.comE
373              
374             =head1 COPYRIGHT AND LICENSE
375              
376             Copyright 2004-2022 by Christian Renz . All rights reserved.
377              
378             This library is free software; you can redistribute it and/or modify
379             it under the same terms as Perl itself.
380              
381             =cut
382              
383             1;