File | /wise/base/static/lib/perl5/site_perl/5.10.0/Module/Find.pm | Statements Executed | 22 | Total Time | 0.001157 seconds |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine | |
---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | Module::Find:: | BEGIN |
0 | 0 | 0 | 0 | 0 | Module::Find:: | _find |
0 | 0 | 0 | 0 | 0 | Module::Find:: | _wanted |
0 | 0 | 0 | 0 | 0 | Module::Find:: | findallmod |
0 | 0 | 0 | 0 | 0 | Module::Find:: | findsubmod |
0 | 0 | 0 | 0 | 0 | Module::Find:: | setmoduledirs |
0 | 0 | 0 | 0 | 0 | Module::Find:: | useall |
0 | 0 | 0 | 0 | 0 | Module::Find:: | usesub |
Line | Stmts. | Exclusive Time | Avg. | Code |
---|---|---|---|---|
1 | package Module::Find; | |||
2 | ||||
3 | 3 | 0.00011 | 3.6e-5 | use 5.006001; |
4 | 3 | 4.1e-5 | 1.4e-5 | use strict; # spent 11µs making 1 call to strict::import |
5 | 3 | 2.6e-5 | 8.7e-6 | use warnings; # spent 23µs making 1 call to warnings::import |
6 | ||||
7 | 3 | 2.6e-5 | 8.7e-6 | use File::Spec; # spent 5µs making 1 call to import |
8 | 3 | 0.00094 | 0.00031 | use File::Find; # spent 56µs making 1 call to Exporter::import |
9 | ||||
10 | 1 | 2.0e-6 | 2.0e-6 | our $VERSION = '0.06'; |
11 | ||||
12 | 1 | 0 | 0 | our $basedir = undef; |
13 | 1 | 1.0e-6 | 1.0e-6 | our @results = (); |
14 | 1 | 1.0e-6 | 1.0e-6 | our $prune = 0; |
15 | ||||
16 | 1 | 5.0e-6 | 5.0e-6 | our @ISA = qw(Exporter); |
17 | ||||
18 | 1 | 1.0e-6 | 1.0e-6 | our @EXPORT = qw(findsubmod findallmod usesub useall setmoduledirs); |
19 | ||||
20 | =head1 NAME | |||
21 | ||||
22 | Module::Find - Find and use installed modules in a (sub)category | |||
23 | ||||
24 | =head1 SYNOPSIS | |||
25 | ||||
26 | use Module::Find; | |||
27 | ||||
28 | # use all modules in the Plugins/ directory | |||
29 | @found = usesub Mysoft::Plugins; | |||
30 | ||||
31 | # use modules in all subdirectories | |||
32 | @found = useall Mysoft::Plugins; | |||
33 | ||||
34 | # find all DBI::... modules | |||
35 | @found = findsubmod DBI; | |||
36 | ||||
37 | # find anything in the CGI/ directory | |||
38 | @found = findallmod CGI; | |||
39 | ||||
40 | # set your own search dirs (uses @INC otherwise) | |||
41 | setmoduledirs(@INC, @plugindirs, $appdir); | |||
42 | ||||
43 | =head1 DESCRIPTION | |||
44 | ||||
45 | Module::Find lets you find and use modules in categories. This can be very | |||
46 | useful for auto-detecting driver or plugin modules. You can differentiate | |||
47 | between looking in the category itself or in all subcategories. | |||
48 | ||||
49 | If you want Module::Find to search in a certain directory on your | |||
50 | harddisk (such as the plugins directory of your software installation), | |||
51 | make sure you modify C<@INC> before you call the Module::Find functions. | |||
52 | ||||
53 | =head1 FUNCTIONS | |||
54 | ||||
55 | =over | |||
56 | ||||
57 | =item C<setmoduledirs(@directories)> | |||
58 | ||||
59 | Sets the directories to be searched for modules. If not set, Module::Find | |||
60 | will use @INC. If you use this function, @INC will I<not> be included | |||
61 | automatically, so add it if you want it. Set to undef to revert to | |||
62 | default behaviour. | |||
63 | ||||
64 | =cut | |||
65 | ||||
66 | sub setmoduledirs { | |||
67 | return @Module::Find::ModuleDirs = @_; | |||
68 | } | |||
69 | ||||
70 | =item C<@found = findsubmod Module::Category> | |||
71 | ||||
72 | Returns modules found in the Module/Category subdirectories of your perl | |||
73 | installation. E.g. C<findsubmod CGI> will return C<CGI::Session>, but | |||
74 | not C<CGI::Session::File> . | |||
75 | ||||
76 | =cut | |||
77 | ||||
78 | sub findsubmod(*) { | |||
79 | $prune = 1; | |||
80 | ||||
81 | return _find($_[0]); | |||
82 | } | |||
83 | ||||
84 | =item C<@found = findallmod Module::Category> | |||
85 | ||||
86 | Returns modules found in the Module/Category subdirectories of your perl | |||
87 | installation. E.g. C<findallmod CGI> will return C<CGI::Session> and also | |||
88 | C<CGI::Session::File> . | |||
89 | ||||
90 | =cut | |||
91 | ||||
92 | sub findallmod(*) { | |||
93 | $prune = 0; | |||
94 | ||||
95 | return _find($_[0]); | |||
96 | } | |||
97 | ||||
98 | =item C<@found = usesub Module::Category> | |||
99 | ||||
100 | Uses and returns modules found in the Module/Category subdirectories of your perl | |||
101 | installation. E.g. C<usesub CGI> will return C<CGI::Session>, but | |||
102 | not C<CGI::Session::File> . | |||
103 | ||||
104 | =cut | |||
105 | ||||
106 | sub usesub(*) { | |||
107 | $prune = 1; | |||
108 | ||||
109 | my @r = _find($_[0]); | |||
110 | ||||
111 | foreach my $m (@r) { | |||
112 | eval " require $m; import $m ; "; | |||
113 | die $@ if $@; | |||
114 | } | |||
115 | ||||
116 | return @r; | |||
117 | } | |||
118 | ||||
119 | =item C<@found = useall Module::Category> | |||
120 | ||||
121 | Uses and returns modules found in the Module/Category subdirectories of your perl | |||
122 | installation. E.g. C<useall CGI> will return C<CGI::Session> and also | |||
123 | C<CGI::Session::File> . | |||
124 | ||||
125 | =cut | |||
126 | ||||
127 | sub useall(*) { | |||
128 | $prune = 0; | |||
129 | ||||
130 | my @r = _find($_[0]); | |||
131 | ||||
132 | foreach my $m (@r) { | |||
133 | eval " require $m; import $m; "; | |||
134 | die $@ if $@; | |||
135 | } | |||
136 | ||||
137 | return @r; | |||
138 | } | |||
139 | ||||
140 | # 'wanted' functions for find() | |||
141 | # you know, this would be a nice application for currying... | |||
142 | sub _wanted { | |||
143 | my $name = File::Spec->abs2rel($_, $basedir); | |||
144 | return unless $name && $name ne File::Spec->curdir(); | |||
145 | ||||
146 | if (-d && $prune) { | |||
147 | $File::Find::prune = 1; | |||
148 | return; | |||
149 | } | |||
150 | ||||
151 | return unless /\.pm$/ && -r; | |||
152 | ||||
153 | $name =~ s|\.pm$||; | |||
154 | $name = join('::', File::Spec->splitdir($name)); | |||
155 | ||||
156 | push @results, $name; | |||
157 | } | |||
158 | ||||
159 | ||||
160 | # helper functions for finding files | |||
161 | ||||
162 | sub _find(*) { | |||
163 | my ($category) = @_; | |||
164 | return undef unless defined $category; | |||
165 | ||||
166 | my $dir = File::Spec->catdir(split(/::/, $category)); | |||
167 | ||||
168 | my @dirs; | |||
169 | if (defined @Module::Find::ModuleDirs) { | |||
170 | @dirs = map { File::Spec->catdir($_, $dir) } | |||
171 | @Module::Find::ModuleDirs; | |||
172 | } else { | |||
173 | @dirs = map { File::Spec->catdir($_, $dir) } @INC; | |||
174 | } | |||
175 | @results = (); | |||
176 | ||||
177 | foreach $basedir (@dirs) { | |||
178 | next unless -d $basedir; | |||
179 | ||||
180 | find({wanted => \&_wanted, | |||
181 | no_chdir => 1}, $basedir); | |||
182 | } | |||
183 | ||||
184 | # filter duplicate modules | |||
185 | my %seen = (); | |||
186 | @results = grep { not $seen{$_}++ } @results; | |||
187 | ||||
188 | @results = map "$category\::$_", @results; | |||
189 | return @results; | |||
190 | } | |||
191 | ||||
192 | =back | |||
193 | ||||
194 | =head1 HISTORY | |||
195 | ||||
196 | =over 8 | |||
197 | ||||
198 | =item 0.01, 2004-04-22 | |||
199 | ||||
200 | Original version; created by h2xs 1.22 | |||
201 | ||||
202 | =item 0.02, 2004-05-25 | |||
203 | ||||
204 | Added test modules that were left out in the first version. Thanks to | |||
205 | Stuart Johnston for alerting me to this. | |||
206 | ||||
207 | =item 0.03, 2004-06-18 | |||
208 | ||||
209 | Fixed a bug (non-localized $_) by declaring a loop variable in use functions. | |||
210 | Thanks to Stuart Johnston for alerting me to this and providing a fix. | |||
211 | ||||
212 | Fixed non-platform compatibility by using File::Spec. | |||
213 | Thanks to brian d foy. | |||
214 | ||||
215 | Added setmoduledirs and updated tests. Idea shamelessly stolen from | |||
216 | ...errm... inspired by brian d foy. | |||
217 | ||||
218 | =item 0.04, 2005-05-20 | |||
219 | ||||
220 | Added POD tests. | |||
221 | ||||
222 | =item 0.05, 2005-11-30 | |||
223 | ||||
224 | Fixed issue with bugfix in PathTools-3.14. | |||
225 | ||||
226 | =item 0.06, 2008-01-26 | |||
227 | ||||
228 | Module::Find now won't report duplicate modules several times anymore (thanks to Uwe Všlker for the report and the patch) | |||
229 | ||||
230 | =back | |||
231 | ||||
232 | =head1 SEE ALSO | |||
233 | ||||
234 | L<perl> | |||
235 | ||||
236 | =head1 AUTHOR | |||
237 | ||||
238 | Christian Renz, E<lt>crenz@web42.comE<gt> | |||
239 | ||||
240 | =head1 COPYRIGHT AND LICENSE | |||
241 | ||||
242 | Copyright 2004-2008 by Christian Renz <crenz@web42.com>. All rights reserved. | |||
243 | ||||
244 | This library is free software; you can redistribute it and/or modify | |||
245 | it under the same terms as Perl itself. | |||
246 | ||||
247 | =cut | |||
248 | ||||
249 | 1 | 8.0e-6 | 8.0e-6 | 1; |