#!/usr/bin/env perl # # renamep - rename(1) but with Perl commands # # $Id$ use warnings; use strict; use Getopt::Std; my %opt; getopts( 'f:hnv', \%opt ); HELP_MESSAGE() if $opt{'h'}; sub HELP_MESSAGE { print STDERR < 1 or oomph('provide at least 2 arguments'); my $code = shift(@ARGV); { local $_ = ''; if ( !defined eval $code ) { $@ =~ s# at .*$/##; oomph( "$code is invalid Perl code", $@ ? ':' : '', $@ ); } } foreach my $f (@ARGV) { local $_ = $f; eval $code; my $g = $_; if ( $g eq $f ) { ehm_v( 'unaffected filename:', $f ); next; } my $action = $opt{f} ? sprintf( $opt{f}, $f, $g ) : "renaming $f to $g"; ehm_v($action); $opt{n} or process( $f, $g ) or ehm( $action, 'failed with error:', $! ); } sub process { if ( $opt{f} ) { !system( sprintf( $opt{f}, @_ ) ); } else { my ( $f, $g ) = @_; # rename(@_) throws a compile error! rename( $f, $g ); } }