| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | =begin metadata | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | Name: rm | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | Description: remove directory entries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | Author: brian d foy, bdfoy@cpan.org | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | License: artistic2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | =end metadata | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | rm - remove directory entries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | rm [-fiPrRv] file ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | =head1 OPTIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | =item * -f - do not prompt the user for each file, and do not consider it an error if a file cannot be removed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | =item * -i - prompt the user for each file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | =item * -P - a no-op, for compatibility. So implementations would overwrite files with random data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | =item * -r - same as -R | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | =item * -R - remove directories recursively | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | =item * -v - show the name of each file after it is removed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | Copyright (c) brian d foy, bdfoy@cpan.org | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | The original version of this program was written by Steve Kemp, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | steve@steve.org.uk, but almost none of that remains. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | =head1 LICENCE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | This program is licensed under the Artistic License 2.0. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | package PerlPowerTools::rm; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | ▶ | use File::Basename; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | ▶ | use File::Spec::Functions; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | use Storable qw(dclone); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | ▶ | use constant EX_SUCCESS => 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 2 | ▶ | use constant EX_FAILURE => 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | ▶ | use constant EX_USAGE => 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 2 | ▶ | use constant OP_SUCCEEDED => 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | ▶ | use constant OP_FAILED => 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 2 | my $Program = basename($0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | ▶ | __PACKAGE__->run( args => \@ARGV ) unless caller; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 10 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=5 SCAR=17.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 10 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 10 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 10 | my $args = delete $args{args}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # This looks funny because the other args are filehandles, which | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # we can't dupe. We want to play with the command-line args such | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # that we don't mess up anything that called us. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 10 | my $self = $class->new( { args => dclone($args), %args } )->process_options; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 10 | ▶ | $self->error( "$Program: -P ignored\n" ) if $self->is_overwrite; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 10 | ▶ | unless ( () = $self->files ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | exit(EX_SUCCESS) if $self->is_force; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $self->error( "$Program: missing argument\n" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | usage(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 10 | my $errors = grep { $self->process_file( $_ ) } $self->files; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 10 | ▶ | exit( $errors ? EX_FAILURE : EX_SUCCESS ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 32 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 32 | my( $class, $args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 32 | bless { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | $class->defaults, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | %$args | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 32 | ▶ | sub defaults { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 32 | my %hash = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | args => [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | error_fh => \*STDERR, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | output_fh => \*STDOUT, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 30 | ▶ | sub files { my $self = shift; @{ $self->{files} } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
files: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 44 | ▶ | sub is_force { my $self = shift; $self->{options}{f} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_force: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 21 | ▶ | sub is_interactive { my $self = shift; $self->{options}{i} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_interactive: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 20 | ▶ | sub is_overwrite { my $self = shift; $self->{options}{P} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_overwrite: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 14 | ▶ | sub is_recursive { my $self = shift; $self->{options}{R} || $self->{options}{r} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_recursive: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 19 | ▶ | sub is_verbose { my $self = shift; $self->{options}{v} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_verbose: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 10 | ▶ | sub options { my $self = shift; $self->{options} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
options: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 32 | ▶ | sub preprocess_options { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
preprocess_options: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 32 | my( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 32 | my @new_args = @{ $self->{args} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 32 | my %args = map { $new_args[$_], $_ } 0 .. $#new_args; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 32 | my @rest; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 32 | ▶ | if( exists $args{'--'} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 9 | @rest = @new_args[ $args{'--'} .. $#new_args ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 9 | @new_args = @new_args[0 .. ($args{'--'} - 1)]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 32 | foreach (@new_args) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 64 | ▶ | if (m/\A\-\-/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | warn "unknown option: '$_'\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | usage(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | # Expand clustering | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | @new_args = map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 32 | ▶ | if( /\A\-(.+)/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 35 | my $cluster = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 35 | map { "-$_" } split //, $cluster; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 29 | $_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | } @new_args; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | # this is rm particular processing: -f and -i turn off each | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | # other, and the last one wins. Figure out which one is last | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | # then filter out all earlier of the other. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 32 | ▶ | if( exists $args{'-f'} && exists $args{'-i'} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $args{"-f"} and exists $args{"-i"}
MC/DC100%2/2
exists $args{"-f"} and exists $args{"-i"}
exists $args{"-f"}
exists $args{"-i"}
Truth table100%3/3
A and B
A= exists $args{"-f"}B= exists $args{"-i"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 6 | my $last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 6 | foreach ( reverse @new_args ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 6 | ▶ | next unless /\A-[fi]\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 6 | $last = $_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 6 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | @new_args = map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 6 | ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 12 | ▶ | ( $last eq '-f' and $_ eq '-i') # f wins | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
$last eq "-f" and $_ eq "-i" or $last ne "-f" and $_ eq "-f"
$last eq "-f" and $_ eq "-i"
$last ne "-f" and $_ eq "-f"
MC/DC75%3/4
$last eq "-f" and $_ eq "-i" or $last ne "-f" and $_ eq "-f"
$last eq "-f"
$_ eq "-i"
$last ne "-f"
$_ eq "-f"
Truth table57%4/7
A and B or C and D
A= $last eq "-f"B= $_ eq "-i"C= $last ne "-f"D= $_ eq "-f"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | ( $last ne '-f' and $_ eq '-f' ) # i wins | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | ) ? () : $_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | } @new_args; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 32 | $self->{original_args} = $self->{args}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 32 | $self->{args} = $self->{preprocessed_args} = [ @new_args, @rest ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 32 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 20 | ▶ | sub process_options { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_options: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 20 | my( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 20 | $self->preprocess_options; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 2 | ▶ | use Getopt::Long qw(:config no_ignore_case); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 20 | my %opts; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | my $ret = Getopt::Long::GetOptionsFromArray( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | $self->{args}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 'f' => \$opts{'f'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 'i' => \$opts{'i'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 'P' => \$opts{'P'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 'R' => \$opts{'R'}, # both of these are recursive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 'r' => \$opts{'r'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 20 | 'v' => \$opts{'v'}, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 20 | ▶ | usage() unless $ret; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 20 | ▶ | $self->{options} = { map { defined $_ ? $_ : 0 } %opts }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 20 | $self->{files} = $self->{args}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 20 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 13 | ▶ | sub process_file { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_file: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 13 | my( $self, $filename ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 13 | my $method = do { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 13 | ▶ | if( -d $filename ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 4 | ▶ | if( ! $self->is_recursive ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 2 | ▶ | $self->error( "$Program: '$filename': is a directory\n" ) unless $self->is_force; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 2 | ▶ | return $self->is_force ? OP_SUCCEEDED : OP_FAILED; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 2 | 'remove_directory'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 9 | 'remove_file'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 11 | my $result = $self->$method( $filename ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 11 | ▶ | return $self->is_force ? OP_SUCCEEDED : $result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 2 | ▶ | sub remove_directory { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove_directory: called CC=12 SCAR=31.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 2 | my( $self, $dirname ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 2 | my $dh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 2 | ▶ | unless( opendir( $dh, $dirname ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | ▶ | $self->error( "$Program: cannot open '$dirname': $!\n" ) unless $self->is_force; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | ▶ | return $self->is_force ? OP_SUCCEEDED : OP_FAILED; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 2 | foreach my $file ( readdir($dh) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 6 | ▶ | next if $file eq '.' || $file eq '..'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$file eq "." or $file eq ".."
MC/DC100%2/2
$file eq "." or $file eq ".."
$file eq "."
$file eq ".."
Truth table100%3/3
A or B
A= $file eq "."B= $file eq ".."
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 2 | my $path = catfile( $dirname, $file ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 2 | ▶ | my $method = -d $path ? 'remove_directory' : 'remove_file'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 2 | my $result = $self->$method($path); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 2 | closedir $dh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 2 | ▶ | unless( rmdir $dirname ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | $self->error( "$Program: cannot remove directory '$dirname': $!\n" ) unless $self->is_force; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | return $self->is_force ? OP_SUCCEEDED : OP_FAILED; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 2 | ▶ | $self->message( "$dirname\n" ) if $self->is_verbose; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 2 | return OP_SUCCEEDED; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 11 | ▶ | sub remove_file { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove_file: called CC=11 SCAR=28.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 11 | my( $self, $filename ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | # Answering no to skip a file is not an error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 11 | ▶ | if( $self->is_interactive ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | $self->message( "$filename: ? " ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | ▶ | return OP_SUCCEEDED if <STDIN> =~ /^[Nn]/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | ▶ | elsif( !$self->is_force && -e $filename && ! -w $filename ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
not $self->is_force and -e $filename and not -w $filename
not $self->is_force and -e $filename
MC/DC0%0/3
not $self->is_force and -e $filename and not -w $filename
not $self->is_force
-e $filename
not -w $filename
Truth table75%3/4
not(A) and B and not(C)
A= not $self->is_forceB= -e $filenameC= not -w $filename
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | $self->message( "$filename: Read-only ? " ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | ▶ | return OP_SUCCEEDED if <STDIN> =~ /^[Nn]/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 11 | ▶ | unless( unlink $filename ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 4 | ▶ | $self->error( "$Program: cannot remove '$filename': $!\n" ) unless $self->is_force; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 4 | ▶ | return $self->is_force ? OP_SUCCEEDED : OP_FAILED; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 7 | ▶ | $self->message( "$filename\n" ) if $self->is_verbose; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 7 | return OP_SUCCEEDED; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | ▶ | sub usage { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usage: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | require Pod::Usage; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | Pod::Usage::pod2usage({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | -exitval => EX_USAGE, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | -verbose => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 4 | ▶ | sub error_fh { my $self = shift; $self->{error_fh} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error_fh: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 4 | ▶ | sub error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 4 | ▶ | print { $self->error_fh || *STDERR } @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->error_fh || *STDERR
MC/DC0%0/2
$self->error_fh || *STDERR
$self->error_fh
*STDERR
Truth table33%1/3
A || B
A= $self->error_fhB= *STDERR
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | ▶ | sub output_fh { my $self = shift; $self->{output_fh} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
output_fh: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | ▶ | sub message { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
message: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | ▶ | print { $self->output_fh || *STDOUT } @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->output_fh || *STDOUT
MC/DC0%0/2
$self->output_fh || *STDOUT
$self->output_fh
*STDOUT
Truth table0%0/3
A || B
A= $self->output_fhB= *STDOUT
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 2 | __PACKAGE__; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||