blib/lib/File/Rename.pm

filter: Statementstmt 86.2% 69 / 80 Branchbran 82.5% 33 / 40 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 82.0% 119 / 145 CC 26 SCAR 22.8
CC
26
Cov
83%
rename_files
13.1
rename
12.3
rename_list
6.1
CRAP 9.7
↑ Summary blib/script/rename »
1 package File::Rename;
2
3 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶BEGIN { eval { require warnings; warnings->import } }
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 17 our @EXPORT_OK = qw( rename );
7 17 our $VERSION = '1.31';
8
9 sub import {
10 7 ▶ require Exporter;
Subroutine import: called CC=1 SCAR=0.0
11 7 our @ISA = qw(Exporter);
12 7 my( $pack ) = @_;
13 7 $pack->export_to_level(1, @_);
14 7 require File::Rename::Options;
15 }
16
17 sub rename_files {
18 36 ▶ my $code = shift;
Subroutine rename_files: called CC=13 SCAR=25.7
Pod documented
19 36 my $options = shift;
20 36 _default(\$options);
21
22 36 my $sub = $code;
23 36 ▶ if ( $options->{unicode_strings} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"unicode_strings"}) 2 34 36
24 2 require File::Rename::Unicode;
25 $sub = File::Rename::Unicode::code($code,
26 2 $options->{encoding});
27 }
28 36 my $errors;
29 36 for (@_) {
30 45 my $was = $_;
31 45 ▶ if ( $options->{filename_only} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'filename_only'}) { } 9 36 45
32 9 require File::Spec;
33 9 my($vol, $dir, $file) = File::Spec->splitpath($_);
34 9 $sub->() for ($file);
35 9 $_ = File::Spec->catpath($vol, $dir, $file);
36 }
37 else {
38 36 $sub->();
39 }
40
41 45 ▶ if( $was eq $_ ){ } # ignore quietly
Branch: 4 branches
BranchTrueFalseTotal
if ($was eq $_) { } 15 30 45
elsif (-e $_ and not $options->{'over_write'}) { } 2 28 30
elsif ($options->{'no_action'}) { } 2 26 28
elsif (CORE::rename $was, $_) { } 25 1 26
42 ▶ elsif( -e $_ and not $options->{over_write} ) {
Condition100%3/3
-e $_ and not $options->{"over_write"}
!ll&&!rl&&r
2442
MC/DC100%2/2
-e $_ and not $options->{"over_write"}
-e $_ not $options->{"over_write"}
Truth table100%3/3
A and not(B)
A= -e $_B= not $options->{"over_write"}
ABresult
0X0
100
111
43 2 ▶ if (/\s/ or $was =~ /\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\s/ or $was =~ /\s/) { } 0 2 2
Condition33%1/3
/\s/ or $was =~ /\s/
l!l&&r!l&&!r
002
MC/DC0%0/2
/\s/ or $was =~ /\s/
/\s/ $was =~ /\s/
Truth table33%1/3
A or B
A= /\s/B= $was =~ /\s/
ABresult
1X1
011
000
44 0 warn "'$was' not renamed: '$_' already exists\n";
45 }
46 else {
47 2 warn "$was not renamed: $_ already exists\n";
48 }
49 2 $errors ++;
50 }
51 elsif( $options->{no_action} ) {
52 2 print "rename($was, $_)\n";
53 }
54 elsif( CORE::rename($was,$_)) {
55 25 ▶ print "$was renamed as $_\n" if $options->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"verbose"} 7 18 25
56 }
57 1 else { warn "Can't rename $was $_: $!\n"; $errors ++; }
58 }
59 36 return !$errors;
60 }
61
62 sub rename_list {
63 5 ▶ my($code, $options, $fh, $file) = @_;
Subroutine rename_list: called CC=6 SCAR=18.0
Pod documented
64 5 _default(\$options);
65 print "Reading filenames from ",
66 ( defined $file ? $file
67 3 ▶ : defined *{$fh}{SCALAR} and
Branch: 1 branch
BranchTrueFalseTotal
defined ${*{$fh;}{'SCALAR'};} ? : 1 2 3
68 3 defined ${*{$fh}{SCALAR}} ? ${*{$fh}{SCALAR}}
69 : "file handle ($fh)"
70 ),
71 5 ▶ "\n" if $options->{verbose};
Branch: 2 branches
BranchTrueFalseTotal
if $options->{"verbose"} 3 2 5
defined $file ? : 0 3 3
Condition33%1/3
(defined $file ? $file : defined *{$fh;}{'SCALAR'}) && (defined ${*{$fh;}{'SCALAR'};} ? ${*{$fh;}{'SCALAR'};} : "file handle ($fh)")
!ll&&!rl&&r
003
MC/DC0%0/2
(defined $file ? $file : defined *{$fh;}{'SCALAR'}) && (defined ${*{$fh;}{'SCALAR'};} ? ${*{$fh;}{'SCALAR'};} : "file handle ($fh)")
(defined $file ? $file : defined *{$fh;}{'SCALAR'}) (defined ${*{$fh;}{'SCALAR'};} ? ${*{$fh;}{'SCALAR'};} : "file handle ($fh)")
Truth table33%1/3
A && B
A= (defined $file ? $file : defined *{$fh;}{'SCALAR'})B= (defined ${*{$fh;}{'SCALAR'};} ? ${*{$fh;}{'SCALAR'};} : "file handle ($fh)")
ABresult
0X0
100
111
72 5 my @file;
73 {
74 5 ▶ local $/ = "\0" if $options->{input_null};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"input_null"} 1 4 5
75 5 chop(@file = <$fh>);
76 }
77 5 rename_files $code, $options, @file;
78 }
79
80 sub rename {
81 14 ▶ my($argv, $code, $verbose) = @_;
Subroutine rename: called CC=8 SCAR=25.1
Pod documented
82 14 ▶ if( ref $code ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $code) 10 4 14
83 10 ▶ if( 'HASH' eq ref $code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" eq ref $code) 10 0 10
84 10 ▶ if(defined $verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $verbose) 0 10 10
85 0 require Carp;
86 0 Carp::carp(<<CARP);
87 File::Rename::rename: third argument ($verbose) ignored
88 CARP
89 }
90 10 $verbose = $code;
91 10 $code = delete $verbose->{_code};
92 10 ▶ unless ( $code ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code) 0 10 10
93 0 require Carp;
94 0 Carp::carp(<<CARP);
95 File::Rename::rename: no _code in $verbose
96 CARP
97 }
98
99 }
100 }
101 14 ▶ unless( ref $code ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $code) 14 0 14
102 14 ▶ if( my $eval = eval <<CODE )
Branch: 1 branch
BranchTrueFalseTotal
if (my $eval = eval "sub {\n$code\n}\n") { } 14 0 14
103 sub {
104 $code
105 }
106 CODE
107 {
108 14 $code = $eval;
109 }
110 else {
111 0 my $error = $@;
112 0 $error =~ s/\b(at\s+)\(eval\s+\d+\)\s/$1/g;
113 0 $error =~ s/(\s+line\s+)(\d+)\b/$1 . ($2-1)/eg;
114 0 $error =~ s/\.?\s*\z/, in:\n$code\n/;
115 0 die $error;
116 }
117 }
118 14 ▶ if( @$argv ) { rename_files $code, $verbose, @$argv }
Branch: 1 branch
BranchTrueFalseTotal
if (@$argv) { } 12 2 14
119 2 else { rename_list $code, $verbose, \*STDIN, 'STDIN' }
120 }
121
122 sub _default {
123 41 ▶ my $ref = shift;
Subroutine _default: called CC=2 SCAR=6.9
124 41 ▶ return if ref $$ref;
Branch: 1 branch
BranchTrueFalseTotal
if ref $$ref 29 12 41
125 12 my $verbose = $$ref;
126 12 $$ref = { verbose => $verbose }
127 }
128
129 17 1;
130
131 __END__
↑ Summary blib/script/rename »