| 1 | package File::Dedup; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Deduplicate files across directories | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | $File::Dedup::VERSION = '0.007'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Digest::SHA; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use feature qw(say); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 5 | my @VALID_OPTIONS = qw(ask directory group recursive); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 14 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=11 SCAR=24.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 14 | my ($class, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | die "Must pass a directory to process" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 14 | ▶ | unless exists $opts{directory}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | die "Supplied directory argument '$opts{directory}' is not a directory" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 13 | ▶ | unless -d $opts{directory}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | warn "Supplied option 'group' not implemented yet" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 12 | ▶ | if exists $opts{group} and defined $opts{group}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $opts{"group"} and defined $opts{"group"}
MC/DC50%1/2
exists $opts{"group"} and defined $opts{"group"}
exists $opts{"group"}
defined $opts{"group"}
Truth table66%2/3
A and B
A= exists $opts{"group"}B= defined $opts{"group"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # do not allow undefined options | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 12 | foreach my $opt ( keys %opts ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | die "Invalid argument '$opt' passed to new" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 23 | ▶ | unless grep { $_ eq $opt } @VALID_OPTIONS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # default to always asking before purging | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | $opts{ask} = 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 11 | ▶ | unless exists $opts{ask} && defined $opts{ask}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $opts{"ask"} and defined $opts{"ask"}
MC/DC50%1/2
exists $opts{"ask"} and defined $opts{"ask"}
exists $opts{"ask"}
defined $opts{"ask"}
Truth table66%2/3
A and B
A= exists $opts{"ask"}B= defined $opts{"ask"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # default to non-recursive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | $opts{recursive} = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 11 | ▶ | unless exists $opts{recursive} && defined $opts{recursive}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $opts{"recursive"} and defined $opts{"recursive"}
MC/DC50%1/2
exists $opts{"recursive"} and defined $opts{"recursive"}
exists $opts{"recursive"}
defined $opts{"recursive"}
Truth table66%2/3
A and B
A= exists $opts{"recursive"}B= defined $opts{"recursive"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 11 | return bless \%opts, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | ▶ | sub directory { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
directory: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 2 | return shift->{directory}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | ▶ | sub recursive { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
recursive: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 2 | return shift->{recursive}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 15 | ▶ | sub ask { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ask: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 15 | return shift->{ask}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub group { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
group: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | return shift->{group}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | ▶ | sub _file_digest { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_file_digest: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 3 | my ($filename) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 3 | ▶ | open my $fh, '<', $filename | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | or die "$!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 3 | my $checksum = Digest::SHA->new->addfile($fh)->hexdigest; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 3 | close($fh); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 3 | return $checksum; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | ▶ | sub dedup { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dedup: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | my @results = $self->_dirwalk( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | $self->directory, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 3 | ▶ | sub { [ $_[0], _file_digest($_[0]) ] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | ▶ | sub { shift; @_ } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 5 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | print Dumper \@results; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 1 | my %files_by_hashsum; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | foreach my $result ( @results ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | my ($filename, $digest) = @$result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 3 | push @{ $files_by_hashsum{$digest} }, $filename; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | my %duplicates_by_hashsum = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | map { $_ => [ sort @{$files_by_hashsum{$_}} ] } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | grep { @{ $files_by_hashsum{$_} } > 1 } keys %files_by_hashsum; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | my @files_to_purge = $self->_handle_duplicates(\%duplicates_by_hashsum); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 1 | $self->_purge_files(\@files_to_purge); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 4 | ▶ | sub _handle_duplicates { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_duplicates: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 4 | my ($self, $duplicates) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 4 | ▶ | return unless keys %$duplicates; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 4 | my @files_to_purge; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 4 | ▶ | while ( my ($digest, $files) = each %$duplicates ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 7 | my $to_keep; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 7 | ▶ | if ( $self->ask ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 4 | say 'The following files are duplicates ' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | . " indicate which one(s) you would like to keep\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | . '(-1 to SKIP or CTRL-C to quit):'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 4 | my $number_of_files = $#{ $files }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 4 | foreach my $i ( 0 .. $number_of_files ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 8 | my $file = $files->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 8 | say "[ $i]\t$file"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 4 | say "[ -1]\tSKIP"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | say "[C-c]\tQUIT"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 4 | $to_keep = _get_numeric_response($number_of_files); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 4 | ▶ | next if ! defined $to_keep || defined $to_keep && $to_keep == -1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
not defined $to_keep or defined $to_keep and $to_keep == -1
defined $to_keep and $to_keep == -1
MC/DC33%1/3
not defined $to_keep or defined $to_keep and $to_keep == -1
not defined $to_keep
defined $to_keep
$to_keep == -1
Truth table50%2/4
not(A) or B and C
A= not defined $to_keepB= defined $to_keepC= $to_keep == -1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | else { # if ask = 0 keep the first duplicate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 3 | $to_keep = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | push @files_to_purge, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 5 | grep { $_ ne $files->[$to_keep] } @$files; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 4 | return sort @files_to_purge; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 2 | ▶ | sub _purge_files { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_purge_files: called CC=8 SCAR=31.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 2 | my ($self, $files) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 2 | foreach my $file ( @$files ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 3 | print "purging file: $file\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 3 | my $response; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 3 | ▶ | if ( $self->ask ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | print "About to delete '$file'; continue? [Y/n] "; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | $response = _prompt(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | while ( !grep { $response eq $_ } ('y', 'Y', 'n', 'N', '') ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 3 | ▶ | _delete_file($file) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition11%1/9
not $self->ask or $self->ask and $response eq "" || $response =~ /^[yY]$/
$self->ask and $response eq "" || $response =~ /^[yY]$/
$response eq "" or $response =~ /^[yY]$/
MC/DC0%0/4
not $self->ask or $self->ask and $response eq "" || $response =~ /^[yY]$/
not $self->ask
$self->ask
$response eq ""
$response =~ /^[yY]$/
Truth table20%1/5
not(A) or B and C or D
A= not $self->askB= $self->askC= $response eq ""D= $response =~ /^[yY]$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | if !$self->ask | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | || ($self->ask | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | && ($response eq '' || $response =~ m/^[yY]$/)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 3 | ▶ | sub _delete_file { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_delete_file: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 3 | my ($file) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 3 | ▶ | unlink($file) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | or die "Unable to delete file '$file': $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 4 | ▶ | sub _get_numeric_response { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_numeric_response: called CC=6 SCAR=20.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 4 | my ($max) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 4 | my $input; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 4 | my $valid_response = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 4 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 4 | print "\n>> "; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 4 | $input = _prompt(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 4 | ▶ | if ( ! defined $input ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | say 'You did not enter any input.'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | elsif ( $input !~ m/^\-?\d+$/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | say "You must enter a number between 0 and $max"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | ▶ | elsif ( $input && $input > $max ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$input and $input > $max
MC/DC0%0/2
$input and $input > $max
$input
$input > $max
Truth table33%1/3
A and B
A= $inputB= $input > $max
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | say "You must enter a number between 0 and $max"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 4 | $valid_response = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } while( !$valid_response ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 4 | print "AFTER get_numeric_response: $input\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 4 | return $input; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | sub _prompt { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_prompt: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | my $input = <STDIN>; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | chomp($input); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | return $input; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 4 | ▶ | sub _dirwalk { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dirwalk: called CC=9 SCAR=23.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 4 | my ($self, $top, $filefunc, $dirfunc) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 4 | ▶ | if ( -d $top ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | # stop processing non-recursive searches when a directory that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | # was not the starting directory is encountered | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 1 | ▶ | if $top ne $self->directory && !$self->recursive; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$top ne $self->directory and not $self->recursive
MC/DC0%0/2
$top ne $self->directory and not $self->recursive
$top ne $self->directory
not $self->recursive
Truth table33%1/3
A and not(B)
A= $top ne $self->directoryB= not $self->recursive
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 1 | my $DIR; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 1 | ▶ | unless ( opendir $DIR, $top ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | warn "Couldn't open directory '$top': $!; skipping.\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 1 | my @results; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 1 | ▶ | while ( my $file = readdir $DIR ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 5 | ▶ | next if $file =~ m/^\./; # ignore hidden files, '.', and '..' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 3 | push @results, $self->_dirwalk("$top/$file", $filefunc, $dirfunc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 1 | ▶ | return $dirfunc ? $dirfunc->($top, @results) : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 3 | ▶ | return $filefunc ? $filefunc->($top) : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||