lib/File/Rename.pm

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