blib/lib/File/List.pm

filter: Statementstmt 68.1% 45 / 66 Branchbran 15.6% 5 / 32 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 87.5% 7 / 8 Podpod 100% 5 / 5 totaltotal 51.2% 64 / 125 CC 22 SCAR 37.1
CC
22
Cov
49%
find
75.6
new
14.5
show_only_dirs
6.0
CRAP 40.7
↑ Summary List/test.pl »
1 package File::List;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 require Exporter;
7  
8 1 @ISA = qw(Exporter);
9 # Items to export into callers namespace by default. Note: do not export
10 # names by default without a very good reason. Use EXPORT_OK instead.
11 # Do not simply export all your public functions/methods/constants.
12 1 @EXPORT = qw(
13
14 );
15 1 $VERSION = '0.3.1';
16  
17 1 my $debug=0;
18  
19 =head1 NAME
20  
21 File::List - Perl extension for crawling directory trees and compiling lists of files
22  
23 =head1 SYNOPSIS
24  
25 use File::List;
26  
27 my $search = new File::List("/usr/local");
28 $search->show_empty_dirs(); # toggle include empty directories in output
29 my @files = @{ $search->find("\.pl\$") }; # find all perl scripts in /usr/local
30  
31 =head1 DESCRIPTION
32  
33 This module crawls the directory tree starting at the provided base directory
34 and can return files (and/or directories if desired) matching a regular expression
35  
36 =cut
37  
38 =head1 INTERFACE
39  
40 The following methods are available in this module.
41  
42 =cut
43  
44 =head2 new($base);
45  
46 This creates a new File::List object and starts crawling the tree from this base
47  
48 It takes a scalar base directory as an argument and returns an object reference
49  
50 =cut
51  
52 1 ▶sub new {
Subroutine new: called CC=7 SCAR=26.8
Pod documented
53 1 my $class = shift;
54 1 my $base = shift;
55 1 my $self = {};
56 1 bless $self, $class;
57  
58 # store my base for later
59 1 $self->{base} = $base;
60  
61 1 ▶ $debug && print "spawned with base [$base]\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "spawned with base [$base]\n" 0 1 1
62  
63 # read in contents of current directory
64 1 opendir (BASE, $base);
65 1 my @entries = grep !/^\.\.?\z/, readdir BASE;
66 1 chomp(@entries);
67 1 closedir(BASE);
68  
69 1 for my $entry (@entries) {
70  
71 # if entry is a directory, launch a new File::List to explore it
72 # and store a reference to the new object in the dirlist hash
73 0 ▶ if (-d "$base/$entry") {
Branch: 2 branches
BranchTrueFalseTotal
if (-d "$base/$entry") { } 0 0 0
elsif (-f "$base/$entry") { } 0 0 0
74 0 ▶ $debug && print _trace(),"following directory $base/$entry\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print _trace(), "following directory $base/$entry\n" 0 0 0
75 0 my $newbase = new File::List("$base/$entry");
76 0 $self->{dirlist}{ $entry } = $newbase;
77 }
78  
79 # if entry is a file, store it's name in the dirlist hash
80 elsif ( -f "$base/$entry"){
81 0 ▶ $debug && print _trace(),"Found file : $base/$entry\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print _trace(), "Found file : $base/$entry\n" 0 0 0
82 0 $self->{dirlist}{ $entry } = 1;
83 }
84 }
85  
86 1 return $self;
87 }
88  
89 =head2 find($regexp);
90  
91 This method accepts a scalar regular expression to search for.
92  
93 It returns a reference to an array containing the full path to files
94 matching the expression (under this base).
95  
96 =cut
97  
98 1 ▶sub find {
Subroutine find: called CC=14 SCAR=43.3
Pod documented
99  
100 1 my $self = shift;
101 1 my $reg = shift;
102 1 my @result = ();
103 1 my $file;
104  
105 1 for my $key (keys %{ $self->{dirlist} } ) {
106  
107 # if we found a reference to a File::List, ask for it's find()
108 0 ▶ if ( ref ( $self->{dirlist}{ $key } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'dirlist'}{$key}) { } 0 0 0
109 0 ▶ $debug && print _trace(),"following directory".$self->{base}."/".$key."\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print _trace(), 'following directory' . $self->{"base"} . '/' . $key . "\n" 0 0 0
110 0 ▶ $self->{showdirs} && $self->{dirlist}{ $key }->show_empty_dirs();
Branch: 1 branch
BranchTrueFalseTotal
$self->{"showdirs"} and $self->{"dirlist"}{$key}->show_empty_dirs 0 0 0
111 0 ▶ $self->{onlydirs} && $self->{dirlist}{ $key }->show_only_dirs();
Branch: 1 branch
BranchTrueFalseTotal
$self->{"onlydirs"} and $self->{"dirlist"}{$key}->show_only_dirs 0 0 0
112 0 push @result, @{ $self->{dirlist}{ $key }->find($reg) };
113 }
114 # ah, found a file, push it into the results (if it matches the regexp)
115 else {
116 0 my $path = $self->{base}."/".$key;
117 0 ▶ $debug && print _trace(),"found file $path\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print _trace(), "found file $path\n" 0 0 0
118 0 ▶ if ( ($path =~ eval{qr/$reg/}) && (! $self->{onlydirs}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ eval { do { qr/$reg/ } } and not $self->{"onlydirs"}) 0 0 0
Condition0%0/3
$path =~ eval { do { qr/$reg/ } } and not $self->{"onlydirs"}
!ll&&!rl&&r
000
MC/DC0%0/2
$path =~ eval { do { qr/$reg/ } } and not $self->{"onlydirs"}
$path =~ eval { do { qr/$reg/ } } not $self->{"onlydirs"}
Truth table0%0/3
A and not(B)
A= $path =~ eval { do { qr/$reg/ } }B= not $self->{"onlydirs"}
ABresult
0X0
100
111
119 0 push @result, ($path);
120 }
121 0 $file++;
122 }
123 }
124  
125
126 1 ▶ if ( ( !$file && $self->{showdirs} || ( $self->{onlydirs} ) ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $file and $self->{"showdirs"} or $self->{"onlydirs"}) 1 0 1
Condition33%2/6
not $file and $self->{"showdirs"} or $self->{"onlydirs"}
l!l&&r!l&&!r
100
not $file and $self->{"showdirs"}
!ll&&!rl&&r
001
MC/DC0%0/3
not $file and $self->{"showdirs"} or $self->{"onlydirs"}
not $file $self->{"showdirs"} $self->{"onlydirs"}
Truth table20%1/5
not(A) and B or C
A= not $fileB= $self->{"showdirs"}C= $self->{"onlydirs"}
ABCresult
11X1
0X11
1011
0X00
1000
127 1 ▶ $debug && print _trace(),"processing dir ".$self->{base}."\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print _trace(), 'processing dir ' . $self->{"base"} . "\n" 1 0 1
128 1 ▶ push @result, ($self->{base}.'/') if ($self->{base} =~ eval {qr/$reg/} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"base"} =~ eval { do { qr/$reg/ } } 0 1 1
129 }
130  
131 # we must be at the bottom level
132 1 return \@result;
133 }
134  
135  
136 =head2 debug($level);
137  
138 This sets the debug level for find
139  
140 =cut
141  
142 1 ▶sub debug {
Subroutine debug: called CC=1 SCAR=0.0
Pod documented
143  
144 1 my $self = shift;
145 1 $debug = shift;
146  
147 1 return 1;
148 }
149  
150 =head2 show_empty_dirs();
151  
152 Toggle display of empty directories
153  
154 =cut
155  
156 1 ▶sub show_empty_dirs {
Subroutine show_empty_dirs: called CC=2 SCAR=7.1
Pod documented
157 1 my $self = shift;
158 1 ▶ $self->{showdirs} = $self->{showdirs}?undef:1;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'showdirs'} ? : 0 1 1
159 1 return 1;
160 }
161  
162 =head2 show_only_dirs();
163  
164 Toggle display of just directories
165  
166 =cut
167  
168 0 ▶sub show_only_dirs {
Subroutine show_only_dirs: not called CC=2 SCAR=17.9
Pod documented
169 0 my $self = shift;
170 0 ▶ $self->{onlydirs} = $self->{onlydirs}?undef:1;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'onlydirs'} ? : 0 0 0
171 0 return 1;
172 }
173  
174 #################
175 # Private methods, not to be used in the public API
176 #################
177  
178 1 ▶sub _trace {
Subroutine _trace: called CC=1 SCAR=0.0
179  
180 1 my @timedat = localtime( time );
181 1 my $timestring = $timedat[ 2 ] . ':' . $timedat[ 1 ] . ':' . $timedat[ 0 ];
182  
183 1 return @{[ ( caller( 1 )) [ 3 ] . "(): " . $timestring . "\t" ]};
184 }
185  
186  
187 1 1;
188 __END__
↑ Summary List/test.pl »