blib/lib/File/Iterator.pm

filter: Statementstmt 25.0% 13 / 52 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 44.4% 4 / 9 Podpod 100% 3 / 3 totaltotal 16.8% 20 / 119 CC 19 SCAR 38.9
CC
19
Cov
14%
next
90.0
new
42.0
_probeDir
42.0
CRAP 49.0
↑ Summary
1 package File::Iterator;
2  
3 1 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 $VERSION = '0.14';
9  
10 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
11 0 my $proto = shift;
12 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
13 0 my $self = {
14 DIR => '.',
15 RECURSE => 1,
16 FILTER => undef,
17 RETURNDIRS => 0,
18 FOLLOWSYMLINKS => 0,
19 @_
20 };
21
22 0 $self->{FILES} = [];
23 0 bless ($self, $class);
24
25 # remove trailing slash unless user has supplied the root directory
26 0 ▶ if ( ! _isRootDir( $self->{DIR} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_isRootDir($self->{"DIR"})) 0 0 0
27 0 ▶ if ( $^O eq "MSWin32" || $^O eq "os2") { # trailing slash can be either / or \
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'MSWin32' or $^O eq 'os2') { } 0 0 0
elsif ($^O eq 'NetWare') { } 0 0 0
Condition0%0/3
$^O eq "MSWin32" or $^O eq "os2"
l!l&&r!l&&!r
000
MC/DC0%0/2
$^O eq "MSWin32" or $^O eq "os2"
$^O eq "MSWin32" $^O eq "os2"
Truth table0%0/3
A or B
A= $^O eq "MSWin32"B= $^O eq "os2"
ABresult
1X1
011
000
28 0 $self->{DIR} =~ s|[\\/]$||;
29 }
30 elsif ( $^O eq "NetWare" ) { # uses \ as directory separator
31 0 $self->{DIR} =~ s|\\$||;
32 }
33 else {
34 0 $self->{DIR} =~ s|/$||;
35 }
36 }
37
38 0 $self->_probeDir( $self->{DIR} );
39 0 return $self;
40 }
41  
42 0 ▶sub _isRootDir {
Subroutine _isRootDir: not called CC=1 SCAR=6.9
43 0 $_[0] =~ m{^(([a-z]:)?[\\/]|\\\\)$}i; # true if arg is /, \, X:\, X:/ or \\
44 }
45  
46 0 ▶sub _probeDir {
Subroutine _probeDir: not called CC=6 SCAR=37.4
47 0 my $self = shift;
48 0 my $dir = shift;
49 0 ▶ my $slash = _isRootDir($dir) ? "" : ( $^O eq "MSWin32" || $^O eq "NetWare" || $^O eq "os2" ) ? "\\" : "/";
Branch: 2 branches
BranchTrueFalseTotal
_isRootDir($dir) ? : 0 0 0
$^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'os2' ? : 0 0 0
Condition0%0/6
$^O eq "MSWin32" or $^O eq "NetWare" or $^O eq "os2"
l!l&&r!l&&!r
000
$^O eq "MSWin32" or $^O eq "NetWare"
l!l&&r!l&&!r
000
MC/DC0%0/3
$^O eq "MSWin32" or $^O eq "NetWare" or $^O eq "os2"
$^O eq "MSWin32" $^O eq "NetWare" $^O eq "os2"
Truth table0%0/4
A or B or C
A= $^O eq "MSWin32"B= $^O eq "NetWare"C= $^O eq "os2"
ABCresult
1XX1
01X1
0011
0000
50  
51 0 ▶ if (opendir DIR, $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir DIR, $dir) { } 0 0 0
52 0 my @files = grep { !/^\.{1,2}$/ } readdir DIR; # ignore . and ..
53 0 unshift @{$self->{FILES}}, map $dir.$slash.$_, sort { lc $a cmp lc $b } @files;
54 0 closedir DIR;
55 }
56 else {
57 0 carp "Can't open $dir: $!";
58 }
59 }
60  
61 0 ▶sub next {
Subroutine next: not called CC=9 SCAR=45.0
Pod documented
62 0 my $self = shift;
63 0 ▶ my $nextfile = shift @{$self->{FILES}} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $nextfile = shift @{$$self{"FILES"};} or return undef 0 0 0
64 0 ▶ if (-d $nextfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $nextfile) 0 0 0
65 # if we are recursing and either the directory is not a symlink or we're following symlinks...
66 0 ▶ if ( $self->{RECURSE} && (!-l $nextfile || $self->{FOLLOWSYMLINKS} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"RECURSE"} and !(-l $nextfile) || $self->{"FOLLOWSYMLINKS"}) 0 0 0
Condition0%0/6
$self->{"RECURSE"} and !(-l $nextfile) || $self->{"FOLLOWSYMLINKS"}
!ll&&!rl&&r
000
not -l $nextfile or $self->{"FOLLOWSYMLINKS"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{"RECURSE"} and !(-l $nextfile) || $self->{"FOLLOWSYMLINKS"}
$self->{"RECURSE"} not -l $nextfile $self->{"FOLLOWSYMLINKS"}
Truth table0%0/4
A and not(B) or C
A= $self->{"RECURSE"}B= not -l $nextfileC= $self->{"FOLLOWSYMLINKS"}
ABCresult
0XX0
1000
11X1
1011
67 0 $self->_probeDir($nextfile);
68 }
69
70 0 ▶ if (!$self->{RETURNDIRS}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"RETURNDIRS"}) 0 0 0
71 0 return $self->next();
72 }
73 }
74  
75 0 my $filter = $self->{FILTER};
76 0 ▶ if ( $filter && !($filter->($nextfile)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter and not &$filter($nextfile)) { } 0 0 0
Condition0%0/3
$filter and not &$filter($nextfile)
!ll&&!rl&&r
000
MC/DC0%0/2
$filter and not &$filter($nextfile)
$filter not &$filter($nextfile)
Truth table0%0/3
A and not(B)
A= $filterB= not &$filter($nextfile)
ABresult
0X0
100
111
77 0 return $self->next();
78 }
79 else {
80 0 return $nextfile;
81 }
82 }
83  
84 0 ▶sub reset {
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
85 0 my $self = shift;
86 0 $self->{FILES} = [];
87 0 $self->_probeDir( $self->{DIR} );
88 }
89  
90 1 1;
91  
92 __END__
↑ Summary