| 1 | package Psh::Builtins::Rename; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | use Psh::Util ':all'; | ||
| 5 | |||
| 6 | =item * C<rename> [-i] perlcode [files] | ||
| 7 | |||
| 8 | "rename" provides the filename in $_ to perlcode | ||
| 9 | and renames according to the new value of $_ modified | ||
| 10 | by perlcode. | ||
| 11 | |||
| 12 | Originally written by Larry Wall | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | |||
| 17 | sub bi_rename | ||
| 18 | { | ||
| 19 | my ($line, $words)= @_; | ||
| 20 | my @words=@$words; | ||
| 21 | my $inspect=0; | ||
| 22 | my $op= shift @words; | ||
| 23 | if ($Psh::interactive && $op eq '-i') { | ||
| 24 | $inspect=1; | ||
| 25 | $op= shift @words; | ||
| 26 | } | ||
| 27 | $op= Psh::Parser::unquote($op); | ||
| 28 | @words = Psh::Parser::glob_expansion(\@words); | ||
| 29 | @words = map { Psh::Parser::unquote($_)} @words; | ||
| 30 | my $count=0; | ||
| 31 | foreach my $file (@words) { | ||
| 32 | unless (-e $file) { | ||
| 33 | print STDERR "$Psh::bin: $file: $!\n"; | ||
| 34 | next; | ||
| 35 | } | ||
| 36 | my $was= $file; | ||
| 37 | $Psh::PerlEval::lastscalar=$was; | ||
| 38 | Psh::PerlEval::protected_eval($op); | ||
| 39 | my $now= $Psh::PerlEval::lastscalar; | ||
| 40 | if ($was ne $now) { | ||
| 41 | if ($inspect && -e $now) { | ||
| 42 | next unless Psh::Util::prompt("yn","remove $now?") eq 'y'; | ||
| 43 | } elsif (-e $now) { | ||
| 44 | print STDERR "$_ exists. $was not renamed\n"; | ||
| 45 | next | ||
| 46 | } | ||
| 47 | if (CORE::rename($was,$now)) { | ||
| 48 | $count++; | ||
| 49 | } else { | ||
| 50 | print STDERR "$Psh::bin: can't rename $was to $now: $!\n"; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | return ($count>0,$count); | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | 1; | ||
| 60 | |||
| 61 | # Local Variables: | ||
| 62 | # mode:perl | ||
| 63 | # tab-width:4 | ||
| 64 | # indent-tabs-mode:t | ||
| 65 | # c-basic-offset:4 | ||
| 66 | # perl-label-offset:0 | ||
| 67 | # perl-indent-level:4 | ||
| 68 | # cperl-indent-level:4 | ||
| 69 | # cperl-label-offset:0 | ||
| 70 | # End: | ||
| 71 |