| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 42 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||
| 43 | 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/
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 68 | 3 | defined ${*{$fh}{SCALAR}} ? ${*{$fh}{SCALAR}} | |||||||||||||||||||||||||||||||
| 69 | : "file handle ($fh)" | ||||||||||||||||||||||||||||||||
| 70 | ), | ||||||||||||||||||||||||||||||||
| 71 | 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)")
| |||||||||||||||||||||||||||||||||
| 72 | 5 | my @file; | |||||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||||||
| 74 | 5 | ▶ | local $/ = "\0" if $options->{input_null}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 83 | 10 | ▶ | if( 'HASH' eq ref $code ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | 10 | ▶ | if(defined $verbose ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 102 | 14 | ▶ | if( my $eval = eval <<CODE ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 125 | 12 | my $verbose = $$ref; | |||||||||||||||||||||||||||||||
| 126 | 12 | $$ref = { verbose => $verbose } | |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 17 | 1; | |||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||||||