#!/usr/bin/perl
#   rename perlexpr [files]
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# My favorite example  is  the  'rename'  script  from  the #
# distribution; it's the fastest way to change all .o files #
# to .o.bak, etc.:                                          #
#   rename 's/$/.bak/' *.o                                  #
# Note that this script will read file names from stdin  if #
# there are none on the command line.                       #
#                                                           #
# BUG:   On some systems, the rename won't occur if the new #
# name exists; on others, the old file with  the  new  name #
# will disappear.  Is there a good solution to this?        #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

($op = shift) || die "Usage: rename perlexpr [filenames]\n";
if (!@ARGV) {
	@ARGV = <STDIN>;
	chop(@ARGV);
}
for $old (@ARGV) {
	$_ = $old;
	eval $op;
#	eval("$new =~ $op");	# Doesn't work.
	die $@ if $@;
	$new = $_;
	if ($new eq $old) {
#		print "$0: $old not changed.\n";
	} else {
		rename($old,$new);
	}
}
