blib/lib/File/Rename.pm

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