File Coverage

blib/lib/File/Rename.pm
Criterion Covered Total %
statement 66 77 85.7
branch 32 38 84.2
condition 5 9 55.5
subroutine 7 7 100.0
pod 3 3 100.0
total 113 134 84.3


line stmt bran cond sub pod time code
1             package File::Rename;
2            
3 23     23   1315546 use 5.032; # use strict; use warnings;
  23         103  
4            
5             require Exporter;
6             our @ISA = qw(Exporter);
7             our @EXPORT_OK = qw( rename );
8            
9             our $VERSION = '2.02';
10            
11             sub import {
12 20     20   112 my $pack = shift;
13 20         50 my($args, $config) = &_config; # sees @_
14 20         1956 $pack->export_to_level(1, $pack, @$args);
15 20         9932 require File::Rename::Options;
16 20         203 File::Rename::Options->import(@$config);
17             }
18            
19             sub rename_files {
20 47     47 1 251770 my $code = shift;
21 47         102 my $options = shift;
22 47         219 _default(\$options);
23            
24 47         122 my $sub = $code;
25 47 100       188 if ( $options->{unicode_strings} ) {
26 3         1844 require File::Rename::Unicode;
27             $sub = File::Rename::Unicode::code($code,
28 3         20 $options->{encoding});
29             }
30 47         84 my $errors;
31 47         196 for (@_) {
32 62         130 my $was = $_;
33 62 100       193 if ( $options->{filename_only} ) {
34 9         125 require File::Spec;
35 9         173 my($vol, $dir, $file) = File::Spec->splitpath($_);
36 9         45 $sub->() for ($file);
37 9         140 $_ = File::Spec->catpath($vol, $dir, $file);
38             }
39             else {
40 53         1148 $sub->();
41             }
42            
43 62 100 100     6404 if( $was eq $_ ){ } # ignore quietly
    100          
    100          
    100          
44             elsif( -e $_ and not $options->{over_write} ) {
45 2 50 33     44 if (/\s/ or $was =~ /\s/ ) {
46 0         0 warn "'$was' not renamed: '$_' already exists\n";
47             }
48             else {
49 2         40 warn "$was not renamed: $_ already exists\n";
50             }
51 2         21 $errors ++;
52             }
53             elsif( $options->{no_action} ) {
54 2         16 print "rename($was, $_)\n";
55             }
56             elsif( CORE::rename($was,$_)) {
57 36 100       273 print "$was renamed as $_\n" if $options->{verbose};
58             }
59 1         23 else { warn "Can't rename $was $_: $!\n"; $errors ++; }
  1         11  
60             }
61 47         572 return !$errors;
62             }
63            
64             sub rename_list {
65 6     6 1 427171 my($code, $options, $fh, $file) = @_;
66 6         77 _default(\$options);
67             print "Reading filenames from ",
68             ( defined $file ? $file
69             : defined *{$fh}{SCALAR} and
70             defined ${*{$fh}{SCALAR}} ? ${*{$fh}{SCALAR}}
71             : "file handle ($fh)"
72             ),
73 6 100 33     78 "\n" if $options->{verbose};
74 6         34 my @file;
75             {
76 6 100       33 local $/ = "\0" if $options->{input_null};
  6         55  
77 6         331 chop(@file = <$fh>);
78             }
79 6         133 rename_files $code, $options, @file;
80             }
81            
82             sub rename {
83 25     25 1 598176 my($argv, $code, $verbose) = @_;
84 25 100       101 if( ref $code ) {
85 21 50       88 if( 'HASH' eq ref $code ) {
86 21 50       62 if(defined $verbose ) {
87 0         0 require Carp;
88 0         0 Carp::carp(<
89             File::Rename::rename: third argument ($verbose) ignored
90             CARP
91             }
92 21         37 $verbose = $code;
93 21         56 $code = delete $verbose->{_code};
94 21 50       64 unless ( $code ) {
95 0         0 require Carp;
96 0         0 Carp::carp(<
97             File::Rename::rename: no _code in $verbose
98             CARP
99             }
100            
101             }
102             }
103 25 50       78 unless( ref $code ) {
104 25 50       4738 if( my $eval = eval <
105             sub {
106             $code
107             }
108             CODE
109             {
110 25         97 $code = $eval;
111             }
112             else {
113 0         0 my $error = $@;
114 0         0 $error =~ s/\b(at\s+)\(eval\s+\d+\)\s/$1/g;
115 0         0 $error =~ s/(\s+line\s+)(\d+)\b/$1 . ($2-1)/eg;
  0         0  
116 0         0 $error =~ s/\.?\s*\z/, in:\n$code\n/;
117 0         0 die $error;
118             }
119             }
120 25 100       93 if( @$argv ) { rename_files $code, $verbose, @$argv }
  22         118  
121 3         181 else { rename_list $code, $verbose, \*STDIN, 'STDIN' }
122             }
123            
124             sub _default {
125 53     53   158 my $ref = shift;
126 53 100       224 return if ref $$ref;
127 12         24 my $verbose = $$ref;
128 12         64 $$ref = { verbose => $verbose }
129             }
130            
131             sub _config {
132             # copied from GetOpt::Long::import
133 20     20   88 my @syms = (); # symbols to import
134 20         41 my @config = (); # configuration
135 20         44 my $dest = \@syms; # symbols first
136 20         61 for ( @_ ) {
137 15 100       51 if ( $_ eq ':config' ) {
138 7         20 $dest = \@config; # config next
139 7         17 next;
140             }
141 8         24 push(@$dest, $_); # push
142             }
143 20         64 return (\@syms, \@config);
144             }
145            
146             1;
147            
148             __END__