| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 44 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||
| 45 | 2 | ▶ | if (/\s/ or $was =~ /\s/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
/\s/u or $was =~ /\s/u
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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 70 | 3 | defined ${*{$fh}{SCALAR}} ? ${*{$fh}{SCALAR}} | |||||||||||||||||||||||||||||||
| 71 | : "file handle ($fh)" | ||||||||||||||||||||||||||||||||
| 72 | ), | ||||||||||||||||||||||||||||||||
| 73 | 6 | ▶ | "\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)")
| |||||||||||||||||||||||||||||||||
| 74 | 6 | my @file; | |||||||||||||||||||||||||||||||
| 75 | { | ||||||||||||||||||||||||||||||||
| 76 | 6 | ▶ | local $/ = "\0" if $options->{input_null}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 85 | 21 | ▶ | if( 'HASH' eq ref $code ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 86 | 21 | ▶ | if(defined $verbose ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 104 | 25 | ▶ | if( my $eval = eval <<CODE ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||