| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 45 | ▶ | elsif( -e $_ and not $options->{over_write} ) { | |||||||||||||||||||||||||||||||
|
Condition100%3/3
-e $_ and not $options->{"over_write"}
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"}
| |||||||||||||||||||||||||||||||||
| 46 | 2 | ▶ | if (/\s/ or $was =~ /\s/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
/\s/ or $was =~ /\s/
MC/DC0%0/2
/\s/ or $was =~ /\s/
/\s/
$was =~ /\s/
Truth table33%1/3
A or B
A= /\s/B= $was =~ /\s/
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 71 | 3 | defined ${*{$fh}{SCALAR}} ? ${*{$fh}{SCALAR}} | |||||||||||||||||||||||||||||||
| 72 | : "file handle ($fh)" | ||||||||||||||||||||||||||||||||
| 73 | ), | ||||||||||||||||||||||||||||||||
| 74 | 5 | ▶ | "\n" if $options->{verbose}; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
(defined $file ? $file : defined *{$fh;}{'SCALAR'}) && (defined ${*{$fh;}{'SCALAR'};} ? ${*{$fh;}{'SCALAR'};} : "file handle ($fh)")
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)")
| |||||||||||||||||||||||||||||||||
| 75 | 5 | my @file; | |||||||||||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||||||||||||
| 77 | 5 | ▶ | local $/ = "\0" if $options->{input_null}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 86 | 9 | ▶ | if( 'HASH' eq ref $code ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 87 | 9 | ▶ | if(defined $verbose ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 105 | 13 | ▶ | if( my $eval = eval <<CODE ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||