blib/lib/Dir/ListFilesRecursive.pm

filter: Statementstmt 79.8% 87 / 109 Branchbran 54.1% 26 / 48 Conditioncond 33.3% 9 / 27 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 90.9% 10 / 11 Podpod 100% 3 / 3 totaltotal 62.5% 135 / 216 CC 39 SCAR 43.4
CC
39
Cov
66%
_filter_file_array
123.2
_sub_path_from_array
6.0
list_files_no_path
4.0
CRAP 76.5
↑ Summary
1 package Dir::ListFilesRecursive; ## Static functions to find files in directories.
2  
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use vars qw(@ISA @EXPORT %EXPORT_TAGS $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION='0.05';
10  
11  
12 2 @ISA = qw(Exporter);
13  
14 2 %EXPORT_TAGS = ( all => [qw(
15 list_files_flat
16 list_files_recursive
17 list_files_no_path
18 )] );
19  
20 2 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 1 ▶sub list_files_flat{ # array|arrayref ($path,%options)
Subroutine list_files_flat: called CC=2 SCAR=7.0
Pod documented
107 1 my $path=shift;
108 1 my %para=@_;
109 1 my @files;
110  
111  
112  
113 1 @files=list_files_no_path($path);
114 1 _add_path_to_array($path,\@files);
115 1 _filter_file_array(\@files,%para);
116  
117 1 @files=sort @files;
118  
119  
120 1 ▶ return wantarray ? @files : \@files;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
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 1 ▶sub list_files_recursive{ # array|arrayref ($path,%options)
Subroutine list_files_recursive: called CC=2 SCAR=7.0
Pod documented
147 1 my $path=shift;
148 1 my %para=@_;
149 1 my @files;
150  
151 1 @files=_list_files_recursive_all($path);
152 1 _filter_file_array(\@files,%para,path=>$path);
153 1 @files=sort @files;
154  
155 1 ▶ return wantarray ? @files : \@files;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
156 }
157  
158  
159  
160  
161 # that is the real recursive function to get all files.
162 12 ▶sub _list_files_recursive_all{
Subroutine _list_files_recursive_all: called CC=3 SCAR=11.0
163 12 my $path=shift;
164 12 my %para=@_;
165 12 my @files;
166 my @filesm;
167  
168  
169 12 @files=list_files_no_path($path);
170  
171 12 _filter_file_array(\@files,%para);
172 12 _add_path_to_array($path,\@files);
173  
174 12 foreach my $d (@files){
175 143 ▶ if (-d $d){
Branch: 1 branch
BranchTrueFalseTotal
if (-d $d) 11 132 143
176 11 push @filesm,_list_files_recursive_all($d);
177 }
178 }
179 12 push @files,@filesm;
180  
181
182
183 12 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 14 ▶sub list_files_no_path{ # array|arrayref ($path,%options)
Subroutine list_files_no_path: called CC=4 SCAR=14.0
Pod documented
203 14 my $path=shift;
204 14 my %para=@_;
205 14 my @files;
206 my @nf;
207  
208 14 opendir(FDIR,$path);
209 14 @files=readdir FDIR;
210 14 closedir(FDIR);
211  
212 14 _filter_file_array(\@files,%para);
213  
214 14 foreach my $d (@files){
215 187 ▶ if ($d!~ m/^\.\.?/){push @nf,$d};
Branch: 1 branch
BranchTrueFalseTotal
unless ($d =~ /^\.\.?/) 187 0 187
216 }
217  
218 14 ▶ return wantarray ? @nf : \@nf;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 14 0 14
219 }
220  
221  
222  
223 # helper method to filter for options.
224 # is filtering the given array with options.
225 28 ▶sub _filter_file_array{
Subroutine _filter_file_array: called CC=30 SCAR=48.1
226 28 my $dir_ref=shift;
227 28 my %para=@_;
228 28 my @nf;
229 28 my $path=$para{path};
230  
231 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
232  
233 0 ▶ if ($para{only_folder} ne ''){$para{no_files}=1};
Branch: 1 branch
BranchTrueFalseTotal
if ($para{"only_folder"} ne "") 0 28 28
234 0 ▶ if ($para{only_folders} ne ''){$para{no_files}=1};
Branch: 1 branch
BranchTrueFalseTotal
if ($para{"only_folders"} ne "") 0 28 28
235 0 ▶ if ($para{only_dir} ne ''){$para{no_files}=1};
Branch: 1 branch
BranchTrueFalseTotal
if ($para{"only_dir"} ne "") 0 28 28
236 0 ▶ if ($para{only_dirs} ne ''){$para{no_files}=1};
Branch: 1 branch
BranchTrueFalseTotal
if ($para{"only_dirs"} ne "") 0 28 28
237 0 ▶ if ($para{only_directories} ne ''){$para{no_files}=1};
Branch: 1 branch
BranchTrueFalseTotal
if ($para{"only_directories"} ne "") 0 28 28
238  
239 0 ▶ if ($para{only_files} ne ''){$para{no_dir}=1};
Branch: 1 branch
BranchTrueFalseTotal
if ($para{"only_files"} ne "") 0 28 28
240
241
242 28 foreach my $i (@$dir_ref){
243 523 my $ok=1;
244 523 ▶ if ($i=~ m/^\.\.?$/){$ok=0};
Branch: 1 branch
BranchTrueFalseTotal
if ($i =~ /^\.\.?$/) 28 495 523
245
246 0 ▶ if (($para{no_files} ne '') && (!-d $i)){$ok=0};
Branch: 1 branch
BranchTrueFalseTotal
if ($para{"no_files"} ne "" and not -d $i) 0 523 523
Condition33%1/3
$para{"no_files"} ne "" and not -d $i
!ll&&!rl&&r
52300
MC/DC0%0/2
$para{"no_files"} ne "" and not -d $i
$para{"no_files"} ne "" not -d $i
Truth table33%1/3
A and not(B)
A= $para{"no_files"} ne ""B= not -d $i
ABresult
0X0
100
111
247 0 ▶ if (($para{no_dir} ne '') && (-d $i)){$ok=0};
Branch: 1 branch
BranchTrueFalseTotal
if ($para{"no_dir"} ne "" and -d $i) 0 523 523
Condition33%1/3
$para{"no_dir"} ne "" and -d $i
!ll&&!rl&&r
52300
MC/DC0%0/2
$para{"no_dir"} ne "" and -d $i
$para{"no_dir"} ne "" -d $i
Truth table33%1/3
A and B
A= $para{"no_dir"} ne ""B= -d $i
ABresult
0X0
100
111
248 0 ▶ if (($para{no_dirs} ne '') && (-d $i)){$ok=0};
Branch: 1 branch
BranchTrueFalseTotal
if ($para{"no_dirs"} ne "" and -d $i) 0 523 523
Condition33%1/3
$para{"no_dirs"} ne "" and -d $i
!ll&&!rl&&r
52300
MC/DC0%0/2
$para{"no_dirs"} ne "" and -d $i
$para{"no_dirs"} ne "" -d $i
Truth table33%1/3
A and B
A= $para{"no_dirs"} ne ""B= -d $i
ABresult
0X0
100
111
249 0 ▶ if (($para{no_directories} ne '') && (-d $i)){$ok=0};
Branch: 1 branch
BranchTrueFalseTotal
if ($para{"no_directories"} ne "" and -d $i) 0 523 523
Condition33%1/3
$para{"no_directories"} ne "" and -d $i
!ll&&!rl&&r
52300
MC/DC0%0/2
$para{"no_directories"} ne "" and -d $i
$para{"no_directories"} ne "" -d $i
Truth table33%1/3
A and B
A= $para{"no_directories"} ne ""B= -d $i
ABresult
0X0
100
111
250 0 ▶ if (($para{no_folder} ne '') && (-d $i)){$ok=0};
Branch: 1 branch
BranchTrueFalseTotal
if ($para{"no_folder"} ne "" and -d $i) 0 523 523
Condition33%1/3
$para{"no_folder"} ne "" and -d $i
!ll&&!rl&&r
52300
MC/DC0%0/2
$para{"no_folder"} ne "" and -d $i
$para{"no_folder"} ne "" -d $i
Truth table33%1/3
A and B
A= $para{"no_folder"} ne ""B= -d $i
ABresult
0X0
100
111
251 0 ▶ if (($para{no_folders} ne '') && (-d $i)){$ok=0};
Branch: 1 branch
BranchTrueFalseTotal
if ($para{"no_folders"} ne "" and -d $i) 0 523 523
Condition33%1/3
$para{"no_folders"} ne "" and -d $i
!ll&&!rl&&r
52300
MC/DC0%0/2
$para{"no_folders"} ne "" and -d $i
$para{"no_folders"} ne "" -d $i
Truth table33%1/3
A and B
A= $para{"no_folders"} ne ""B= -d $i
ABresult
0X0
100
111
252 0 ▶ if (($para{no_hidden_files} ne '') && ($i=~ m/^\./)){$ok=0};
Branch: 1 branch
BranchTrueFalseTotal
if ($para{"no_hidden_files"} ne "" and $i =~ /^\./) 0 523 523
Condition33%1/3
$para{"no_hidden_files"} ne "" and $i =~ /^\./
!ll&&!rl&&r
52300
MC/DC0%0/2
$para{"no_hidden_files"} ne "" and $i =~ /^\./
$para{"no_hidden_files"} ne "" $i =~ /^\./
Truth table33%1/3
A and B
A= $para{"no_hidden_files"} ne ""B= $i =~ /^\./
ABresult
0X0
100
111
253 0 ▶ if (($para{no_hidden} ne '') && ($i=~ m/^\./)){$ok=0};
Branch: 1 branch
BranchTrueFalseTotal
if ($para{"no_hidden"} ne "" and $i =~ /^\./) 0 523 523
Condition33%1/3
$para{"no_hidden"} ne "" and $i =~ /^\./
!ll&&!rl&&r
52300
MC/DC0%0/2
$para{"no_hidden"} ne "" and $i =~ /^\./
$para{"no_hidden"} ne "" $i =~ /^\./
Truth table33%1/3
A and B
A= $para{"no_hidden"} ne ""B= $i =~ /^\./
ABresult
0X0
100
111
254  
255 523 ▶ my $ext=lc($para{ext}) || lc($para{extension});
Condition33%1/3
lc $para{'ext'} || lc $para{'extension'}
l!l&&r!l&&!r
00523
MC/DC0%0/2
lc $para{'ext'} || lc $para{'extension'}
lc $para{'ext'} lc $para{'extension'}
Truth table33%1/3
A || B
A= lc $para{'ext'}B= lc $para{'extension'}
ABresult
1X1
011
000
256 523 ▶ if (exists $para{ext}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $para{"ext"}) 0 523 523
257 0 ▶ if ($i=~ m/\.$ext$/i){$ok=1}else{$ok=0};
Branch: 1 branch
BranchTrueFalseTotal
if ($i =~ /\.$ext$/i) { } 0 0 0
258 };
259  
260 523 ▶ if ($ok == 1){push @nf,$i};
Branch: 1 branch
BranchTrueFalseTotal
if ($ok == 1) 495 28 523
261 }
262 28 @$dir_ref=@nf;
263 28 undef @nf;
264  
265 28 ▶ if ($para{no_path} ne ''){
Branch: 1 branch
BranchTrueFalseTotal
if ($para{"no_path"} ne "") 0 28 28
266 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 13 ▶sub _add_path_to_array{
Subroutine _add_path_to_array: called CC=2 SCAR=6.9
277 13 my $path=shift;
278 13 my $dir_ref=shift;
279  
280 13 foreach my $z (@$dir_ref){
281 165 $z=$path.'/'.$z;
282 }
283 }
284  
285  
286 # helper method to remove path from found files
287 0 ▶sub _sub_path_from_array{
Subroutine _sub_path_from_array: not called CC=2 SCAR=17.9
288 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 2 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<Exporter>
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  
↑ Summary