#!/usr/bin/perl
# xre - perl script to rename version-controlled files with PERL expressions
# (c) 2009 by Fantasai
# Interface based on Larry Wall's wonderful 'rename' command
# This is version 3.
#
# This script is open source under the BSD License:
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the original author nor the names of any contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
if (!@ARGV || "-h" eq $ARGV[0] || "-help" eq $ARGV[0] || "--help" eq $ARGV[0]) {
print "Extended Rename:\n";
print " xre [OPTION] COMMAND PERLEXPR [list of strings]\n";
print "COMMAND is a command to run.\n";
print "PERLEXPR is a perl expression that modifies the \$_ string.\n";
print "For each argument after PERLEXPR that is modified by the perl\n" .
"expression, the command will be executed with the original\n" .
"argument and the argument as altered by the perl expression.\n" .
"Alternatively, the list of strings may be given on stdin.\n";
print "The -v option prints the command in addition to executing it.\n";
print " eRename -v 'svn mv' 's/perl/expr/' *\n";
print "The -p option prints the command instead of executing it.\n";
print " eRename -p 'svn mv' 's/perl/expr/' *\n";
exit;
}
if ('-v' eq $ARGV[0]) {
$print = 1;
$exec = 1;
shift @ARGV;
}
elsif ('-p' eq $ARGV[0]) {
$print = 1;
$exec = 0;
shift @ARGV;
}
else {
$print = 0;
$exec = 1;
}
$cmd = shift @ARGV;
$expr = shift @ARGV;
if (@ARGV) {
@strings = @ARGV;
}
else {
@strings = ();
while () {
chomp;
push @strings, $_;
}
}
foreach (@strings) {
$old = $_;
eval $expr;
if ($old ne $_) {
print "$cmd $old $_\n" if $print;
print `$cmd $old $_` if $exec;
}
}