File Coverage

blib/lib/Module/Find.pm
Criterion Covered Total %
statement 67 67 100.0
branch 18 24 75.0
condition 7 9 77.7
subroutine 14 14 100.0
pod 7 7 100.0
total 113 121 93.3


line stmt bran cond sub pod time code
1             package Module::Find;
2              
3 10     10   622615 use 5.006001;
  10         104  
4 10     10   53 use strict;
  10         14  
  10         236  
5 10     10   89 use warnings;
  10         26  
  10         265  
6              
7 10     10   68 use File::Spec;
  10         19  
  10         318  
8 10     10   74 use File::Find;
  10         24  
  10         12191  
9              
10             our $VERSION = '0.14';
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 4     4 1 2243 return @Module::Find::ModuleDirs = grep { defined } @_;
  3         14  
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 1827 $prune = 1;
94            
95 9         33 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 5062 $prune = 0;
108            
109 10         33 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 2     2 1 87 $prune = 1;
124            
125 2         7 my @r = _find($_[0]);
126            
127 2         7 foreach my $m (@r) {
128 2         173 eval " require $m; import $m ; ";
129 2 50       12 die $@ if $@;
130             }
131            
132 2         9 return @r;
133             }
134              
135             =item C<@found = useall Module::Category>
136              
137             Uses and returns modules found in the Module/Category subdirectories of your perl installation. E.g. C will return C and also
138             C .
139              
140             If any module dies during loading, useall will also die at this point.
141              
142             =cut
143              
144             sub useall(*) {
145 4     4 1 1406 $prune = 0;
146            
147 4         17 my @r = _find($_[0]);
148            
149 4         11 foreach my $m (@r) {
150 7         482 eval " require $m; import $m; ";
151 7 50       38 die $@ if $@;
152             }
153            
154 4         20 return @r;
155             }
156              
157             # 'wanted' functions for find()
158             # you know, this would be a nice application for currying...
159             sub _wanted {
160 64     64   4177 my $name = File::Spec->abs2rel($_, $basedir);
161 64 100 66     2449 return unless $name && $name ne File::Spec->curdir() && substr($name, 0, 1) ne '.';
      66        
162              
163 44 100 100     825 if (-d && $prune) {
164 8         22 $File::Find::prune = 1;
165 8         125 return;
166             }
167              
168 36 100       933 return unless /\.pm$/;
169              
170 27         115 $name =~ s|\.pm$||;
171 27         150 $name = join('::', File::Spec->splitdir($name));
172              
173 27         1185 push @results, $name;
174             }
175              
176              
177             # helper functions for finding files
178              
179             sub _find(*) {
180 25     25   56 my ($category) = @_;
181 25 50       69 return undef unless defined $category;
182              
183 25         320 my $dir = File::Spec->catdir(split(/::|'/, $category));
184              
185 25         74 @results = ();
186              
187 25 100       91 foreach my $inc (@Module::Find::ModuleDirs ? @Module::Find::ModuleDirs : @INC) {
188 209 100       486 if (ref $inc) {
189 1 50       3 if (my @files = eval { $inc->files }) {
  1         4  
190             push @results,
191 1 50       20 map { s/^\Q$category\E::// ? $_ : () }
192 1         4 map { s{/}{::}g; s{\.pm$}{}; $_ }
  1         18  
  1         4  
193 1         12 grep { /\.pm$/ }
  1         7  
194             @files;
195             }
196             }
197             else {
198 208         1063 our $basedir = File::Spec->catdir($inc, $dir);
199              
200 208 100       2658 next unless -d $basedir;
201 20         2904 find({wanted => \&_wanted,
202             no_chdir => 1,
203             follow => $followMode}, $basedir);
204             }
205             }
206              
207             # filter duplicate modules
208 25         71 my %seen = ();
209 25         102 @results = grep { not $seen{$_}++ } @results;
  28         131  
210              
211 25         111 @results = map "$category\::$_", @results;
212              
213             @results = map {
214 25 50       64 ($_ =~ m{^(\w+(?:(?:::|')\w+)*)$})[0] || die "$_ does not look like a package name"
  26         221  
215             } @results;
216              
217 25         247 return @results;
218             }
219              
220             =item C
221              
222             Do not follow symlinks. This function is not exported by default.
223              
224             =cut
225              
226             sub ignoresymlinks {
227 1     1 1 872 $followMode = 0;
228             }
229              
230             =item C
231              
232             Follow symlinks (default behaviour). This function is not exported by default.
233              
234             =cut
235              
236             sub followsymlinks {
237 1     1 1 293 $followMode = 1;
238             }
239              
240             =back
241              
242             =head1 HISTORY
243              
244             =over 8
245              
246             =item 0.01, 2004-04-22
247              
248             Original version; created by h2xs 1.22
249              
250             =item 0.02, 2004-05-25
251              
252             Added test modules that were left out in the first version. Thanks to
253             Stuart Johnston for alerting me to this.
254              
255             =item 0.03, 2004-06-18
256              
257             Fixed a bug (non-localized $_) by declaring a loop variable in use functions.
258             Thanks to Stuart Johnston for alerting me to this and providing a fix.
259              
260             Fixed non-platform compatibility by using File::Spec.
261             Thanks to brian d foy.
262              
263             Added setmoduledirs and updated tests. Idea shamelessly stolen from
264             ...errm... inspired by brian d foy.
265              
266             =item 0.04, 2005-05-20
267              
268             Added POD tests.
269              
270             =item 0.05, 2005-11-30
271              
272             Fixed issue with bugfix in PathTools-3.14.
273              
274             =item 0.06, 2008-01-26
275              
276             Module::Find now won't report duplicate modules several times anymore (thanks to Uwe Völker for the report and the patch)
277              
278             =item 0.07, 2009-09-08
279              
280             Fixed RT#38302: Module::Find now follows symlinks by default (can be disabled).
281              
282             =item 0.08, 2009-09-08
283              
284             Fixed RT#49511: Removed Mac OS X extended attributes from distribution
285              
286             =item 0.09, 2010-02-26
287              
288             Fixed RT#38302: Fixed META.yml generation (thanks very much to cpanservice for the help).
289              
290             =item 0.10, 2010-02-26
291              
292             Fixed RT#55010: Removed Unicode BOM from Find.pm.
293              
294             =item 0.11, 2012-05-22
295              
296             Fixed RT#74251: defined(@array) is deprecated under Perl 5.15.7.
297             Thanks to Roman F, who contributed the implementation.
298              
299             =item 0.12, 2014-02-08
300              
301             Fixed RT#81077: useall fails in taint mode
302             Thanks to Aran Deltac, who contributed the implementation and test.
303              
304             Fixed RT#83596: Documentation doesn't describe behaviour if a module fails to load
305             Clarified documentation for useall and usesub.
306              
307             Fixed RT#62923: setmoduledirs(undef) doesn't reset to searching @INC
308             Added more explicit tests.
309             Thanks to Colin Robertson for his input.
310              
311             =item 0.13, 2015-03-09
312              
313             This release contains two contributions from Moritz Lenz:
314             - Link to Module::Pluggable and Class::Factory::Util in "SEE ALSO"
315             - Align package name parsing with how perl does it (allowing single quotes as module separator)
316              
317             Added test for meta.yml
318              
319             =item 0.14, 2019-12-25
320              
321             A long overdue update. Thank you for the many contributions!
322              
323             - Fixed RT#99055: Removed file readability check (pull request contributed by Moritz Lenz)
324             - Now supports @INC hooks (pull request contributed by Graham Knop)
325             - Now filters out filenames starting with a dot (pull request contributed by Desmond Daignault)
326             - Now uses strict (pull request contributed by Shlomi Fish)
327             - Fixed RT#122016: test/ files show up in metacpan (bug report contributed by Karen Etheridge)
328              
329             =back
330              
331             =head1 DEVELOPMENT NOTES
332              
333             Please report any bugs using the CPAN RT system. The development repository for this module is hosted on GitHub: L.
334              
335             =head1 SEE ALSO
336              
337             L, L, L
338              
339             =head1 AUTHOR
340              
341             Christian Renz, Ecrenz@web42.comE
342              
343             =head1 COPYRIGHT AND LICENSE
344              
345             Copyright 2004-2014 by Christian Renz . All rights reserved.
346              
347             This library is free software; you can redistribute it and/or modify
348             it under the same terms as Perl itself.
349              
350             =cut
351              
352             1;