Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.85k stars 527 forks source link

[PATCH] eg/rename #1732

Closed p5pRT closed 20 years ago

p5pRT commented 24 years ago

Migrated from rt.perl.org#2983 (status was 'resolved')

Searchable as RT2983$

p5pRT commented 24 years ago

From @ask

I modernized eg/rename a little. (use strict\, podified the documentation\, added a few options\, fixed the one bug it had).

I'm not sure we care. If we do then I'll look into fixing up some more in that directory. If we don't then it should all go away.

Inline Patch ```diff --- /usr/src/perl-5.6.0/eg/rename Tue Jul 20 10:17:53 1999 +++ rename Sun Apr 2 22:10:21 2000 @@ -1,74 +1,99 @@ -#!/usr/bin/perl -'di'; -'ig00'; +#!/usr/bin/perl -w +use strict; +use Getopt::Long; # -# $RCSfile: rename,v $$Revision: 4.1 $$Date: 92/08/07 17:20:30 $ +# Venerable but very handy utility for bulk renaming of files # -# $Log: rename,v $ -($op = shift) || die "Usage: rename perlexpr [filenames]\n"; +my %opts = ( + ); +GetOptions (\%opts, + "force!", + "quiet!", + "verbose!", + ); + + +my $op = shift or die "Usage: rename perlexpr [filenames]\n"; if (!@ARGV) { @ARGV = ; chop(@ARGV); } for (@ARGV) { - $was = $_; + my $was = $_; eval $op; die $@ if $@; - rename($was,$_) unless $was eq $_; + print "renaming $was -> $_\n" if $opts{verbose}; + if (-e $_ and !$opts{force}) { + warn "Did not rename $was to $_ - target already exists\n" unless $opts{quiet}; + next; + } + rename($was,$_) || warn "rename($was,$_): $!\n" unless $was eq $_; } -############################################################################## - # These next few lines are legal in both Perl and nroff. +=head1 NAME + +rename - renames multiple files + +=head1 SYNOPSIS + +rename perlexpr [files] + +=head1 DESCRIPTION + +Rename renames the filenames supplied according to the rule specified +as the first argument. The argument is a Perl expression which is +expected to modify the $_ string in Perl for at least some of the +filenames specified. If a given filename is not modified by the +expression, it will not be renamed. If no filenames are given on the +command line, filenames will be read via standard input. -.00; # finish .ig - -'di \" finish diversion--previous line must be blank -.nr nl 0-1 \" fake up transition to first page again -.nr % 0 \" start at page 1 -';<<'.ex'; #__END__ ############# From here on it's a standard manual page ############ -.TH RENAME 1 "July 30, 1990" -.AT 3 -.SH NAME -rename \- renames multiple files -.SH SYNOPSIS -.B rename perlexpr [files] -.SH DESCRIPTION -.I Rename -renames the filenames supplied according to the rule specified as the -first argument. -The argument is a Perl expression which is expected to modify the $_ -string in Perl for at least some of the filenames specified. -If a given filename is not modified by the expression, it will not be -renamed. -If no filenames are given on the command line, filenames will be read -via standard input. -.PP -For example, to rename all files matching *.bak to strip the extension, -you might say -.nf +For example, to rename all files matching *.bak to strip the +extension, you might say - rename 's/\e.bak$//' *.bak + rename 's/\.bak$//' *.bak -.fi To translate uppercase names to lower, you'd use -.nf - rename 'y/A-Z/a-z/' * + rename 'y/A-Z/a-z/' * + +=head1 OPTIONS + +All options can be abbreviated where it makes sense (ie. use -v for +--verbose, -f for --force etc - see L). + +=over 4 + +=item --force + +rename the file even if the target exists. + +=item --verbose + +be loud about what we're doing. + +=item --quiet + +don't warn us when we skip a file. + +=back + +=head1 ENVIRONMENT -.fi -.SH ENVIRONMENT No environment variables are used. -.SH FILES -.SH AUTHOR -Larry Wall -.SH "SEE ALSO" -mv(1) -.br -perl(1) -.SH DIAGNOSTICS + +=head1 AUTHOR + +Larry Wall (in 1990). Modernized slightly (use strict and -w, pod +documentation, fixed the one bug, etc) by Ask Bjoern Hansen +Eask@netcetera.dkE. + +=head1 SEE ALSO + +perl(1), mv(1) + +=head1 DIAGNOSTICS + If you give an invalid Perl expression you'll get a syntax error. -.SH BUGS -.I Rename -does not check for the existence of target filenames, so use with care. -.ex + + - ask ```
p5pRT commented 24 years ago

From @jandubois

On Sun\, 2 Apr 2000 22​:11​:50 -0700 (PDT)\, Ask Bjoern Hansen \ask@&#8203;valueclick\.com wrote​:

I modernized eg/rename a little. (use strict\, podified the documentation\, added a few options\, fixed the one bug it had).

I'm not sure we care. If we do then I'll look into fixing up some more in that directory. If we don't then it should all go away.

Yes\, eg/ should have been deleted for the 5.6 release​:

http​://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-08/msg01012.html

eg/ directory to be deleted.

-Jan

p5pRT commented 24 years ago

From [Unknown Contact. See original ticket]

On Sun\, Apr 02\, 2000 at 10​:53​:58PM -0700\, Jan Dubois wrote​:

On Sun\, 2 Apr 2000 22​:11​:50 -0700 (PDT)\, Ask Bjoern Hansen \ask@&#8203;valueclick\.com wrote​:

I modernized eg/rename a little. (use strict\, podified the documentation\, added a few options\, fixed the one bug it had).

I'm not sure we care. If we do then I'll look into fixing up some more in that directory. If we don't then it should all go away.

Yes\, eg/ should have been deleted for the 5.6 release​:

But the work on rename is much appreciated. It's something that belongs in a Unix toolbox. Debian even installs it in /usr/bin as part of the perl package.