File Coverage

blib/lib/Dir/ListFilesRecursive.pm
Criterion Covered Total %
statement 82 104 78.8
branch 26 48 54.1
condition 9 27 33.3
subroutine 10 11 90.9
pod 3 3 100.0
total 130 193 67.3


line stmt bran cond sub pod time code
1             package Dir::ListFilesRecursive; ## Static functions to find files in directories.
2              
3              
4 2     2   1736 use strict;
  2         2  
  2         50  
5              
6 2     2   5 use vars qw(@ISA @EXPORT %EXPORT_TAGS $VERSION);
  2         2  
  2         120  
7 2     2   19 use Exporter;
  2         1  
  2         743  
8              
9             our $VERSION='0.05';
10              
11              
12             @ISA = qw(Exporter);
13              
14             %EXPORT_TAGS = ( all => [qw(
15             list_files_flat
16             list_files_recursive
17             list_files_no_path
18             )] );
19              
20             Exporter::export_ok_tags('all');
21              
22              
23             # This class provides static functions which can be imported to the namespace of
24             # the current class. The functions lists the content of directories.
25             #
26             # With options you can filter the files for specific criteria, like no hidden files, etc.
27             #
28             # SYNOPSIS
29             # ========
30             #
31             # # imports all functions
32             # use Dir::ListFilesRecursive ':all';
33             #
34             # # imports only one function
35             # use Dir::ListFilesRecursive qw( list_files_recursive );
36             #
37             # use Data::Dumper;
38             # print Dumper( list_files_recursive('/etc') );
39             #
40             # use Data::Dumper;
41             # print Dumper( list_files_recursive('/etc', only_folders => 1) );
42             # # shows only subfolders of /etc
43             #
44             #
45             # Options
46             # =======
47             #
48             # For some functions, you can set options like the only_folders in the SYNOPSIS's example.
49             # You can use the following options:
50             # As options you can set these flags:
51             #
52             # only_folders => 1,
53             # only_files => 1,
54             # no_directories => 1,
55             # no_folders => 1,
56             # no_hidden_files => 1,
57             # extension => 'string',
58             # no_path => 1,
59             #
60             # You can also use various aliases:
61             #
62             # only_folders:
63             # only_folder, only_dir, only_dirs, only_directories, no_files
64             #
65             # no_directories:
66             # no_dir, no_dirs, no_folder, no_folders
67             #
68             # no_hidden_files:
69             # no_hidden
70             #
71             # extension:
72             # ext
73             #
74             # Not implemented so far: regular expression match, file age and other attributes.
75             #
76             #
77             #
78             # LICENSE
79             # =======
80             # You can redistribute it and/or modify it under the conditions of LGPL.
81             #
82             # AUTHOR
83             # ======
84             # Andreas Hernitscheck ahernit(AT)cpan.org
85              
86              
87              
88              
89              
90              
91             # List the files of a directory with full path.
92             #
93             # print list_files_flat('/etc');
94             # # may return files like:
95             # # /etc/hosts
96             # # /etc/passwd
97             #
98             # It does not return directory names. (that means 'flat'),
99             # only the files of given directory.
100             #
101             # You can set key value pairs to use further options.
102             # Please see chapter 'options'.
103             #
104             # It returns an array or arrayref, depending on context.
105             #
106             sub list_files_flat{ # array|arrayref ($path,%options)
107 1     1 1 12598 my $path=shift;
108 1         2 my %para=@_;
109 1         2 my @files;
110              
111              
112              
113 1         4 @files=list_files_no_path($path);
114 1         3 _add_path_to_array($path,\@files);
115 1         2 _filter_file_array(\@files,%para);
116              
117 1         8 @files=sort @files;
118              
119              
120 1 50       9 return wantarray ? @files : \@files;
121             }
122              
123              
124              
125              
126              
127              
128              
129              
130              
131              
132             # List the files of a directory and subdirctories
133             # with full path.
134             #
135             # print list_files_recursive('/etc');
136             # # may return files like:
137             # # /etc/hosts
138             # # /etc/passwd
139             # # /etc/apache/httpd.conf
140             #
141             # You can set key value pairs to use further options.
142             # Please see chapter 'options'.
143             #
144             # It returns an array or arrayref, depending on context.
145             #
146             sub list_files_recursive{ # array|arrayref ($path,%options)
147 1     1 1 38 my $path=shift;
148 1         2 my %para=@_;
149 1         1 my @files;
150              
151 1         2 @files=_list_files_recursive_all($path);
152 1         6 _filter_file_array(\@files,%para,path=>$path);
153 1         35 @files=sort @files;
154              
155 1 50       19 return wantarray ? @files : \@files;
156             }
157              
158              
159              
160              
161             # that is the real recursive function to get all files.
162             sub _list_files_recursive_all{
163 12     12   9 my $path=shift;
164 12         13 my %para=@_;
165 12         8 my @files;
166             my @filesm;
167              
168              
169 12         12 @files=list_files_no_path($path);
170              
171 12         18 _filter_file_array(\@files,%para);
172 12         13 _add_path_to_array($path,\@files);
173              
174 12         13 foreach my $d (@files){
175 143 100       1234 if (-d $d){
176 11         17 push @filesm,_list_files_recursive_all($d);
177             }
178             }
179 12         19 push @files,@filesm;
180              
181            
182            
183 12         69 return @files;
184             }
185              
186              
187              
188              
189             # List the files of a directory without the path.
190             #
191             # print list_files_no_path('/etc');
192             # # may return files like:
193             # # hosts
194             # # passwd
195             #
196             # It does not return directory names.
197             #
198             # You can set key value pairs to use further options.
199             # Please see chapter 'options'.
200             #
201             # It returns an array or arrayref, depending on context.
202             sub list_files_no_path{ # array|arrayref ($path,%options)
203 14     14 1 48 my $path=shift;
204 14         15 my %para=@_;
205 14         10 my @files;
206             my @nf;
207              
208 14         194 opendir(FDIR,$path);
209 14         288 @files=readdir FDIR;
210 14         67 closedir(FDIR);
211              
212 14         25 _filter_file_array(\@files,%para);
213              
214 14         13 foreach my $d (@files){
215 187 50       205 if ($d!~ m/^\.\.?/){push @nf,$d};
  187         134  
216             }
217              
218 14 50       57 return wantarray ? @nf : \@nf;
219             }
220              
221              
222              
223             # helper method to filter for options.
224             # is filtering the given array with options.
225             sub _filter_file_array{
226 28     28   25 my $dir_ref=shift;
227 28         26 my %para=@_;
228 28         20 my @nf;
229 28         21 my $path=$para{path};
230              
231 2     2   8 no warnings;
  2         2  
  2         724  
232              
233 28 50       46 if ($para{only_folder} ne ''){$para{no_files}=1};
  0         0  
234 28 50       35 if ($para{only_folders} ne ''){$para{no_files}=1};
  0         0  
235 28 50       35 if ($para{only_dir} ne ''){$para{no_files}=1};
  0         0  
236 28 50       37 if ($para{only_dirs} ne ''){$para{no_files}=1};
  0         0  
237 28 50       32 if ($para{only_directories} ne ''){$para{no_files}=1};
  0         0  
238              
239 28 50       31 if ($para{only_files} ne ''){$para{no_dir}=1};
  0         0  
240            
241            
242 28         30 foreach my $i (@$dir_ref){
243 523         302 my $ok=1;
244 523 100       591 if ($i=~ m/^\.\.?$/){$ok=0};
  28         24  
245            
246 523 50 33     683 if (($para{no_files} ne '') && (!-d $i)){$ok=0};
  0         0  
247 523 50 33     673 if (($para{no_dir} ne '') && (-d $i)){$ok=0};
  0         0  
248 523 50 33     666 if (($para{no_dirs} ne '') && (-d $i)){$ok=0};
  0         0  
249 523 50 33     628 if (($para{no_directories} ne '') && (-d $i)){$ok=0};
  0         0  
250 523 50 33     632 if (($para{no_folder} ne '') && (-d $i)){$ok=0};
  0         0  
251 523 50 33     640 if (($para{no_folders} ne '') && (-d $i)){$ok=0};
  0         0  
252 523 50 33     636 if (($para{no_hidden_files} ne '') && ($i=~ m/^\./)){$ok=0};
  0         0  
253 523 50 33     646 if (($para{no_hidden} ne '') && ($i=~ m/^\./)){$ok=0};
  0         0  
254              
255 523   33     1020 my $ext=lc($para{ext}) || lc($para{extension});
256 523 50       535 if (exists $para{ext}){
257 0 0       0 if ($i=~ m/\.$ext$/i){$ok=1}else{$ok=0};
  0         0  
  0         0  
258             };
259              
260 523 100       535 if ($ok == 1){push @nf,$i};
  495         422  
261             }
262 28         102 @$dir_ref=@nf;
263 28         42 undef @nf;
264              
265 28 50       57 if ($para{no_path} ne ''){
266 0         0 _sub_path_from_array($path,$dir_ref);
267             }
268              
269             }
270              
271              
272              
273              
274              
275             # helper method to add the path to the found files.
276             sub _add_path_to_array{
277 13     13   13 my $path=shift;
278 13         7 my $dir_ref=shift;
279              
280 13         12 foreach my $z (@$dir_ref){
281 165         166 $z=$path.'/'.$z;
282             }
283             }
284              
285              
286             # helper method to remove path from found files
287             sub _sub_path_from_array{
288 0     0     my $path=shift;
289 0           my $dir_ref=shift;
290              
291 0           foreach my $z (@$dir_ref){
292 0           $z=~ s/^$path\/?//;
293             }
294             }
295              
296              
297              
298              
299             1;
300             #################### pod generated by Pod::Autopod - keep this line to make pod updates possible ####################
301              
302             =head1 NAME
303              
304             Dir::ListFilesRecursive - Static functions to find files in directories.
305              
306              
307             =head1 SYNOPSIS
308              
309              
310             # imports all functions
311             use Dir::ListFilesRecursive ':all';
312              
313             # imports only one function
314             use Dir::ListFilesRecursive qw( list_files_recursive );
315              
316             use Data::Dumper;
317             print Dumper( list_files_recursive('/etc') );
318              
319             use Data::Dumper;
320             print Dumper( list_files_recursive('/etc', only_folders => 1) );
321             # shows only subfolders of /etc
322              
323              
324              
325              
326             =head1 DESCRIPTION
327              
328             This class provides static functions which can be imported to the namespace of
329             the current class. The functions lists the content of directories.
330              
331             With options you can filter the files for specific criteria, like no hidden files, etc.
332              
333              
334              
335             =head1 REQUIRES
336              
337             L
338              
339              
340             =head1 METHODS
341              
342             =head2 list_files_flat
343              
344             my @array | \@arrayref = list_files_flat($path, %options);
345              
346             List the files of a directory with full path.
347              
348             print list_files_flat('/etc');
349             # may return files like:
350             # /etc/hosts
351             # /etc/passwd
352              
353             It does not return directory names. (that means 'flat'),
354             only the files of given directory.
355              
356             You can set key value pairs to use further options.
357             Please see chapter 'options'.
358              
359             It returns an array or arrayref, depending on context.
360              
361              
362              
363             =head2 list_files_no_path
364              
365             my @array | \@arrayref = list_files_no_path($path, %options);
366              
367             List the files of a directory without the path.
368              
369             print list_files_no_path('/etc');
370             # may return files like:
371             # hosts
372             # passwd
373              
374             It does not return directory names.
375              
376             You can set key value pairs to use further options.
377             Please see chapter 'options'.
378              
379             It returns an array or arrayref, depending on context.
380              
381              
382             =head2 list_files_recursive
383              
384             my @array | \@arrayref = list_files_recursive($path, %options);
385              
386             List the files of a directory and subdirctories
387             with full path.
388              
389             print list_files_recursive('/etc');
390             # may return files like:
391             # /etc/hosts
392             # /etc/passwd
393             # /etc/apache/httpd.conf
394              
395             You can set key value pairs to use further options.
396             Please see chapter 'options'.
397              
398             It returns an array or arrayref, depending on context.
399              
400              
401              
402              
403             =head1 Options
404              
405              
406             For some functions, you can set options like the only_folders in the SYNOPSIS's example.
407             You can use the following options:
408             As options you can set these flags:
409              
410             only_folders => 1,
411             only_files => 1,
412             no_directories => 1,
413             no_folders => 1,
414             no_hidden_files => 1,
415             extension => 'string',
416             no_path => 1,
417              
418             You can also use various aliases:
419              
420             only_folders:
421             only_folder, only_dir, only_dirs, only_directories, no_files
422              
423             no_directories:
424             no_dir, no_dirs, no_folder, no_folders
425              
426             no_hidden_files:
427             no_hidden
428              
429             extension:
430             ext
431              
432             Not implemented so far: regular expression match, file age and other attributes.
433              
434              
435              
436              
437              
438             =head1 AUTHOR
439              
440             Andreas Hernitscheck ahernit(AT)cpan.org
441              
442              
443             =head1 LICENSE
444              
445             You can redistribute it and/or modify it under the conditions of LGPL.
446              
447              
448              
449             =cut
450