Skip to content

Commit 9ae7db6

Browse files
Dennis OpackiGitpan
Dennis Opacki
authored and
Gitpan
committed
Import of DOPACKI/File-List-0.2 from CPAN.
gitpan-cpan-distribution: File-List gitpan-cpan-version: 0.2 gitpan-cpan-path: DOPACKI/File-List-0.2.tar.gz gitpan-cpan-author: DOPACKI gitpan-cpan-maturity: released
1 parent 366b838 commit 9ae7db6

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

File-List-0.1.tar.gz

-2.44 KB
Binary file not shown.

File-List-0.2.tar.gz

2.67 KB
Binary file not shown.

List.pm

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ require Exporter;
1212
@EXPORT = qw(
1313
1414
);
15-
$VERSION = '0.1';
15+
$VERSION = '0.2';
1616

1717
my $debug=0;
18+
my $showdirs=0;
1819

1920
=head1 NAME
2021
@@ -25,12 +26,13 @@ File::List - Perl extension for crawling directory trees and compiling lists of
2526
use File::List;
2627
2728
my $search = new File::List("/usr/local");
29+
$search->show_empty_dirs(); # toggle include empty directories in output
2830
my @files = @{ $search->find("\.pl\$") }; # find all perl scripts in /usr/local
2931
3032
=head1 DESCRIPTION
3133
3234
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
3436
3537
=cut
3638

@@ -76,7 +78,7 @@ sub new {
7678
}
7779

7880
# if entry is a file, store it's name in the dirlist hash
79-
elsif ( -f "$base/$entry") {
81+
elsif ( -f "$base/$entry"){
8082
$debug && print _trace(),"Found file : $base/$entry\n";
8183
$self->{dirlist}{ $entry } = 1;
8284
}
@@ -99,21 +101,31 @@ sub find {
99101
my $self = shift;
100102
my $reg = shift;
101103
my @result = ();
104+
my $file;
102105

103106
for my $key (keys %{ $self->{dirlist} } ) {
104107

105108
# if we found a reference to a File::List, ask for it's find()
106109
if ( ref ( $self->{dirlist}{ $key } ) ) {
107110
$debug && print _trace(),"following directory".$self->{base}."/".$key."\n";
111+
$self->{showdirs} && $self->{dirlist}{ $key }->show_empty_dirs();
108112
push @result, @{ $self->{dirlist}{ $key }->find($reg) };
109113
}
110114
# ah, found a file, push it into the results (if it matches the regexp)
111115
else {
112116
my $path = $self->{base}."/".$key;
113117
$debug && print _trace(),"found file $path\n";
114118
push @result, ($path) if ($path =~ eval{qr/$reg/} );
119+
$file++;
115120
}
116121
}
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+
117129
# we must be at the bottom level
118130
return \@result;
119131
}
@@ -133,6 +145,17 @@ sub debug {
133145
return 1;
134146
}
135147

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+
}
136159

137160
#################
138161
# Private methods, not to be used in the public API

README

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ SYNOPSIS
66
use File::List;
77

88
my $search = new File::List("/usr/local");
9+
$search->show_empty_dirs(); # toggle include empty directories in output
910
my @files = @{ $search->find("\.pl\$") }; # find all perl scripts in /usr/local
1011

1112
DESCRIPTION
1213
This module crawls the directory tree starting at the provided
13-
base directory and can return files matching a regular
14-
expression
14+
base directory and can return files (and directories if desired)
15+
matching a regular expression
1516

1617
INTERFACE
1718
The following methods are available in this module.
@@ -35,6 +36,10 @@ INTERFACE
3536

3637
This sets the debug level for find
3738

39+
show_empty_dirs();
40+
41+
Toggle display of empty directories
42+
3843
AUTHOR
3944
Dennis Opacki, dopacki@internap.com
4045

test.pl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@
1818
# (correspondingly "not ok 13") depending on the success of chunk 13
1919
# of the test code):
2020

21-
my $test = new File::List("/usr/local");
21+
my $test = new File::List("/usr/opt");
2222

23-
my @perl_scripts = @{ $test->find("pl\$") };
23+
#$test->show_empty_dirs();
24+
my @perl_scripts = @{ $test->find("music") };
2425
print join("\n",@perl_scripts);
25-
print "\n";
26+
print "\n-----------------\n";
27+
28+
$test->show_empty_dirs();
29+
my @perl_scripts = @{ $test->find("music") };
30+
print join("\n",@perl_scripts);
31+
print "\n-----------------\n";

0 commit comments

Comments
 (0)