line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
3
|
|
|
3
|
|
27
|
use Perlmazing;
|
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
18
|
|
2
|
3
|
|
|
3
|
|
22
|
use File::Spec;
|
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
2850
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
sub main {
|
5
|
5
|
|
|
5
|
0
|
24
|
my ($path, $recursive, $callback);
|
6
|
5
|
|
|
|
|
10
|
my $usage = 'Usage: dir ($path, $boolean_recursive, $coderef_callback)';
|
7
|
5
|
|
|
|
|
14
|
my @coderefs = grep { isa_code $_ } @_;
|
|
10
|
|
|
|
|
31
|
|
8
|
5
|
|
|
|
|
14
|
my @args = grep { not isa_code $_ } @_;
|
|
10
|
|
|
|
|
22
|
|
9
|
5
|
50
|
|
|
|
21
|
croak "More than one coderef received in arguments, don't know which one to use as callback - $usage" if @coderefs > 1;
|
10
|
5
|
50
|
|
|
|
16
|
croak "Too many non coderef arguments received - $usage" if @args > 2;
|
11
|
5
|
|
|
|
|
10
|
my $wantarray = wantarray;
|
12
|
5
|
|
|
|
|
11
|
$callback = shift @coderefs;
|
13
|
5
|
|
|
|
|
11
|
($path, $recursive) = ('.', 0);
|
14
|
5
|
|
|
|
|
15
|
@_ = @args;
|
15
|
5
|
100
|
|
|
|
25
|
if (@_ == 1) {
|
|
|
50
|
|
|
|
|
|
16
|
1
|
50
|
|
|
|
22
|
if (-d $_[0]) {
|
17
|
1
|
|
|
|
|
4
|
$path = $_[0];
|
18
|
|
|
|
|
|
|
} else {
|
19
|
0
|
0
|
|
|
|
0
|
$recursive = $_[0] ? 1 : 0;
|
20
|
|
|
|
|
|
|
}
|
21
|
|
|
|
|
|
|
} elsif (@_ == 2) {
|
22
|
4
|
50
|
33
|
|
|
86
|
if (defined($_[0]) and -d $_[0]) {
|
|
|
0
|
0
|
|
|
|
|
23
|
4
|
|
|
|
|
13
|
$path = $_[0];
|
24
|
4
|
|
|
|
|
6
|
$recursive = $_[1];
|
25
|
|
|
|
|
|
|
} elsif (defined($_[1]) and -d $_[1]) {
|
26
|
0
|
|
|
|
|
0
|
$path = $_[1];
|
27
|
0
|
|
|
|
|
0
|
$recursive = $_[0];
|
28
|
|
|
|
|
|
|
} else {
|
29
|
0
|
|
|
|
|
0
|
croak "None of your parameters seems to be a valid/readable path";
|
30
|
|
|
|
|
|
|
}
|
31
|
|
|
|
|
|
|
}
|
32
|
5
|
|
|
|
|
71
|
$path = File::Spec->catdir(File::Spec->splitdir($path));
|
33
|
5
|
|
|
|
|
25
|
_dir($path, $recursive, $callback, $wantarray);
|
34
|
|
|
|
|
|
|
}
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _dir {
|
37
|
26
|
|
|
26
|
|
65
|
my ($path, $recursive, $callback, $wantarray) = @_;
|
38
|
26
|
50
|
|
|
|
767
|
if (opendir my $d, $path) {
|
39
|
26
|
|
|
|
|
76
|
my (@folders, @files, @results);
|
40
|
|
|
|
|
|
|
my $process = sub {
|
41
|
44
|
|
|
44
|
|
65
|
my $i = shift;
|
42
|
44
|
100
|
|
|
|
131
|
$callback->($i) if $callback;
|
43
|
44
|
100
|
|
|
|
168
|
push (@results, $i) if $wantarray;
|
44
|
26
|
|
|
|
|
133
|
};
|
45
|
26
|
|
|
|
|
586
|
for my $i (sort numeric readdir $d) {
|
46
|
96
|
|
|
|
|
622
|
my $item = File::Spec->catdir($path, $i);
|
47
|
96
|
100
|
|
|
|
1356
|
if (-d $item) {
|
48
|
75
|
100
|
100
|
|
|
374
|
next if $i eq '.' or $i eq '..';
|
49
|
23
|
|
|
|
|
77
|
push @folders, $item;
|
50
|
|
|
|
|
|
|
} else {
|
51
|
21
|
|
|
|
|
83
|
push @files, $item;
|
52
|
|
|
|
|
|
|
}
|
53
|
|
|
|
|
|
|
}
|
54
|
26
|
|
|
|
|
80
|
for my $i (@folders) {
|
55
|
23
|
|
|
|
|
62
|
$process->($i);
|
56
|
23
|
100
|
|
|
|
45
|
if ($recursive) {
|
57
|
21
|
|
|
|
|
75
|
my @r = _dir($i, $recursive, $callback, $wantarray);
|
58
|
21
|
100
|
|
|
|
93
|
push (@results, @r) if $wantarray;
|
59
|
|
|
|
|
|
|
}
|
60
|
|
|
|
|
|
|
}
|
61
|
26
|
|
|
|
|
43
|
for my $i (@files) {
|
62
|
21
|
|
|
|
|
42
|
$process->($i);
|
63
|
|
|
|
|
|
|
}
|
64
|
|
|
|
|
|
|
|
65
|
26
|
100
|
|
|
|
303
|
return @results if $wantarray
|
66
|
|
|
|
|
|
|
} else {
|
67
|
0
|
|
|
|
|
0
|
warn "Cannot read path $path: $!";
|
68
|
|
|
|
|
|
|
}
|
69
|
10
|
|
|
|
|
121
|
return;
|
70
|
|
|
|
|
|
|
}
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
1;
|