Front page | perl.perl5.porters |
Postings from April 2000
[PATCH] eg/rename
Thread Previous
|
Thread Next
From:
Ask Bjoern Hansen
Date:
April 2, 2000 22:11
Subject:
[PATCH] eg/rename
Message ID:
Pine.LNX.4.10.10004022207240.12082-100000@impatience.valueclick.com
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.
--- /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 = <STDIN>;
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<Getopt::Long>).
+
+=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
+E<lt>ask@netcetera.dkE<gt>.
+
+=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
--
ask bjoern hansen - <http://www.netcetera.dk/~ask/>
more than 70M impressions per day, <http://valueclick.com>
Thread Previous
|
Thread Next