| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
#!/usr/bin/perl -w |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package File::PerlMove; |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
# Author : Johan Vromans |
|
6
|
|
|
|
|
|
|
# Created On : Tue Sep 15 15:59:04 1992 |
|
7
|
|
|
|
|
|
|
# Last Modified By: Johan Vromans |
|
8
|
|
|
|
|
|
|
# Last Modified On: Tue Apr 11 11:32:06 2017 |
|
9
|
|
|
|
|
|
|
# Update Count : 173 |
|
10
|
|
|
|
|
|
|
# Status : Unknown, Use with caution! |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
################ Common stuff ################ |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = "1.00"; |
|
15
|
|
|
|
|
|
|
|
|
16
|
4
|
|
|
4
|
|
69815
|
use strict; |
|
|
4
|
|
|
|
|
5
|
|
|
|
4
|
|
|
|
|
107
|
|
|
17
|
4
|
|
|
4
|
|
16
|
use warnings; |
|
|
4
|
|
|
|
|
5
|
|
|
|
4
|
|
|
|
|
113
|
|
|
18
|
4
|
|
|
4
|
|
15
|
use Carp; |
|
|
4
|
|
|
|
|
7
|
|
|
|
4
|
|
|
|
|
319
|
|
|
19
|
4
|
|
|
4
|
|
20
|
use File::Basename; |
|
|
4
|
|
|
|
|
6
|
|
|
|
4
|
|
|
|
|
328
|
|
|
20
|
4
|
|
|
4
|
|
23
|
use File::Path; |
|
|
4
|
|
|
|
|
9
|
|
|
|
4
|
|
|
|
|
2373
|
|
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub move { |
|
23
|
10
|
|
|
10
|
0
|
7690
|
my $transform = shift; |
|
24
|
10
|
|
|
|
|
16
|
my $filelist = shift; |
|
25
|
10
|
|
100
|
|
|
36
|
my $options = shift || {}; |
|
26
|
10
|
|
|
|
|
16
|
my $result = 0; |
|
27
|
|
|
|
|
|
|
|
|
28
|
10
|
50
|
33
|
|
|
62
|
croak("Usage: move(" . |
|
29
|
|
|
|
|
|
|
"operation, [ file names ], { options })") |
|
30
|
|
|
|
|
|
|
unless defined $transform && defined $filelist; |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# For those who misunderstood the docs. |
|
33
|
10
|
|
33
|
|
|
57
|
$options->{showonly} ||= delete $options->{'dry-run'}; |
|
34
|
10
|
|
66
|
|
|
38
|
$options->{createdirs} ||= delete $options->{'create-dirs'}; |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Create transformer. |
|
37
|
10
|
100
|
|
|
|
37
|
$transform = build_sub($transform) |
|
38
|
|
|
|
|
|
|
unless ref($transform) eq 'CODE'; |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# Process arguments. |
|
41
|
10
|
50
|
|
|
|
25
|
@$filelist = reverse(@$filelist) if $options->{reverse}; |
|
42
|
10
|
|
|
|
|
21
|
foreach ( @$filelist ) { |
|
43
|
|
|
|
|
|
|
# Save the name. |
|
44
|
10
|
|
|
|
|
13
|
my $old = $_; |
|
45
|
|
|
|
|
|
|
# Perform the transformation. |
|
46
|
10
|
|
|
|
|
182
|
$transform->(); |
|
47
|
|
|
|
|
|
|
# Get the new name. |
|
48
|
10
|
|
|
|
|
32
|
my $new = $_; |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# Anything changed? |
|
51
|
10
|
50
|
|
|
|
28
|
unless ( $old eq $new ) { |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Create directories. |
|
54
|
10
|
100
|
|
|
|
24
|
if ( $options->{createdirs} ) { |
|
55
|
1
|
|
|
|
|
46
|
my $dir = dirname($new); |
|
56
|
1
|
50
|
|
|
|
9
|
unless ( -d $dir ) { |
|
57
|
1
|
50
|
|
|
|
2
|
if ( $options->{showonly} ) { |
|
58
|
0
|
|
|
|
|
0
|
warn("[Would create: $dir]\n"); |
|
59
|
|
|
|
|
|
|
} |
|
60
|
|
|
|
|
|
|
else { |
|
61
|
1
|
|
|
|
|
103
|
mkpath($dir, $options->{verbose}, 0777); |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# Dry run. |
|
67
|
10
|
50
|
33
|
|
|
46
|
if ( $options->{verbose} || $options->{showonly} ) { |
|
68
|
0
|
|
|
|
|
0
|
warn("$old => $new\n"); |
|
69
|
0
|
0
|
|
|
|
0
|
next if $options->{showonly}; |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# Check for overwriting target. |
|
73
|
10
|
100
|
66
|
|
|
175
|
if ( ! $options->{overwrite} && -e $new ) { |
|
74
|
2
|
|
|
|
|
43
|
warn("$new: exists\n"); |
|
75
|
2
|
|
|
|
|
17
|
next; |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# Perform. |
|
79
|
8
|
|
|
|
|
13
|
my $res = -1; |
|
80
|
8
|
100
|
|
|
|
25
|
if ( $options->{symlink} ) { |
|
|
|
100
|
|
|
|
|
|
|
81
|
1
|
|
|
|
|
29
|
$res = symlink($old, $new); |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
elsif ( $options->{link} ) { |
|
84
|
1
|
|
|
|
|
27
|
$res = link($old, $new); |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
else { |
|
87
|
6
|
|
|
|
|
166
|
$res = rename($old, $new); |
|
88
|
|
|
|
|
|
|
} |
|
89
|
8
|
100
|
|
|
|
21
|
if ( $res == 1 ) { |
|
90
|
7
|
|
|
|
|
16
|
$result++; |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
else { |
|
93
|
|
|
|
|
|
|
# Force error numbers (for locale independency). |
|
94
|
|
|
|
|
|
|
warn($options->{errno} |
|
95
|
1
|
50
|
|
|
|
22
|
? "$old: ".(0+$!)."\n" |
|
96
|
|
|
|
|
|
|
: "$old: $!\n"); |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
} |
|
100
|
|
|
|
|
|
|
|
|
101
|
10
|
|
|
|
|
101
|
$result; |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub build_sub { |
|
105
|
9
|
|
|
9
|
0
|
13
|
my $cmd = shift; |
|
106
|
|
|
|
|
|
|
# Special treatment for some. |
|
107
|
9
|
100
|
|
|
|
54
|
if ( $cmd =~ /^(uc|lc|ucfirst)$/ ) { |
|
|
|
50
|
|
|
|
|
|
|
108
|
2
|
|
|
|
|
5
|
$cmd = '$_ = ' . $cmd; |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
elsif ( $cmd =~ /^:(.+):(.+):$/ ) { |
|
111
|
0
|
|
|
|
|
0
|
require Encode; |
|
112
|
0
|
|
|
|
|
0
|
$cmd = 'Encode::from_to($_,"'.$1.'","'.$2.'")'; |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# Build subroutine. |
|
116
|
9
|
|
|
|
|
762
|
my $op = eval "sub { $cmd }"; |
|
117
|
9
|
50
|
|
|
|
29
|
if ( $@ ) { |
|
118
|
0
|
|
|
|
|
0
|
$@ =~ s/ at \(eval.*/./; |
|
119
|
0
|
|
|
|
|
0
|
croak($@); |
|
120
|
|
|
|
|
|
|
} |
|
121
|
|
|
|
|
|
|
|
|
122
|
9
|
|
|
|
|
15
|
return $op; |
|
123
|
|
|
|
|
|
|
} |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
1; |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
__END__ |