| 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
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | if ( $^O eq "MSWin32" || $^O eq "os2") { # trailing slash can be either / or \ | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$^O eq "MSWin32" or $^O eq "os2"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
$^O eq "MSWin32" or $^O eq "NetWare" or $^O eq "os2"
$^O eq "MSWin32" or $^O eq "NetWare"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | if (opendir DIR, $dir) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if (-d $nextfile) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
$self->{"RECURSE"} and !(-l $nextfile) || $self->{"FOLLOWSYMLINKS"}
not -l $nextfile or $self->{"FOLLOWSYMLINKS"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | $self->_probeDir($nextfile); | |||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | if (!$self->{RETURNDIRS}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | return $self->next(); | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my $filter = $self->{FILTER}; | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ( $filter && !($filter->($nextfile)) ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$filter and not &$filter($nextfile)
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)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||