@@ -12,9 +12,10 @@ require Exporter;
12
12
@EXPORT = qw(
13
13
14
14
) ;
15
- $VERSION = ' 0.1 ' ;
15
+ $VERSION = ' 0.2 ' ;
16
16
17
17
my $debug =0;
18
+ my $showdirs =0;
18
19
19
20
=head1 NAME
20
21
@@ -25,12 +26,13 @@ File::List - Perl extension for crawling directory trees and compiling lists of
25
26
use File::List;
26
27
27
28
my $search = new File::List("/usr/local");
29
+ $search->show_empty_dirs(); # toggle include empty directories in output
28
30
my @files = @{ $search->find("\.pl\$") }; # find all perl scripts in /usr/local
29
31
30
32
=head1 DESCRIPTION
31
33
32
34
This module crawls the directory tree starting at the provided base directory
33
- and can return files matching a regular expression
35
+ and can return files (and directories if desired) matching a regular expression
34
36
35
37
=cut
36
38
@@ -76,7 +78,7 @@ sub new {
76
78
}
77
79
78
80
# if entry is a file, store it's name in the dirlist hash
79
- elsif ( -f " $base /$entry " ) {
81
+ elsif ( -f " $base /$entry " ){
80
82
$debug && print _trace()," Found file : $base /$entry \n " ;
81
83
$self -> {dirlist }{ $entry } = 1;
82
84
}
@@ -99,21 +101,31 @@ sub find {
99
101
my $self = shift ;
100
102
my $reg = shift ;
101
103
my @result = ();
104
+ my $file ;
102
105
103
106
for my $key (keys %{ $self -> {dirlist } } ) {
104
107
105
108
# if we found a reference to a File::List, ask for it's find()
106
109
if ( ref ( $self -> {dirlist }{ $key } ) ) {
107
110
$debug && print _trace()," following directory" .$self -> {base }." /" .$key ." \n " ;
111
+ $self -> {showdirs } && $self -> {dirlist }{ $key }-> show_empty_dirs();
108
112
push @result , @{ $self -> {dirlist }{ $key }-> find($reg ) };
109
113
}
110
114
# ah, found a file, push it into the results (if it matches the regexp)
111
115
else {
112
116
my $path = $self -> {base }." /" .$key ;
113
117
$debug && print _trace()," found file $path \n " ;
114
118
push @result , ($path ) if ($path =~ eval {qr /$reg / } );
119
+ $file ++;
115
120
}
116
121
}
122
+
123
+
124
+ if (!$file && $self -> {showdirs }) {
125
+ $debug && print _trace()," found empty dir " .$self -> {base }." \n " ;
126
+ push @result , ($self -> {base }) if ($self -> {base } =~ eval {qr /$reg / } );
127
+ }
128
+
117
129
# we must be at the bottom level
118
130
return \@result ;
119
131
}
@@ -133,6 +145,17 @@ sub debug {
133
145
return 1;
134
146
}
135
147
148
+ =head2 show_empty_dirs();
149
+
150
+ Toggle display of empty directories
151
+
152
+ =cut
153
+
154
+ sub show_empty_dirs {
155
+ my $self = shift ;
156
+ $self -> {showdirs } = $self -> {showdirs }?undef :1;
157
+ return 1;
158
+ }
136
159
137
160
# ################
138
161
# Private methods, not to be used in the public API
0 commit comments