develooper Front page | perl.beginners | Postings from January 2011

Re: Renaming with a specific spec

Thread Previous | Thread Next
From:
John W. Krahn
Date:
January 31, 2011 23:24
Subject:
Re: Renaming with a specific spec
Message ID:
4D47B511.3080401@shaw.ca
Jim Gibson wrote:
>
> You are not likely to find such a specific tool. However, you can
> develop your own tool.
>
> Here is a renaming program adapted from the one in the Perl Cookbook,
> Recipe 9.9:
>
> #!/usr/bin/perl
> #
> # rename
> #
> # perl script to rename files
> #
> # Usage:
> #
> # rename perlexpr [files]
> #
>
> ($op = shift) ||
> die
> "Usage: rename perlexpr [filenames]
> where perlexpr is any perl expression that modifies \$\_:
> 's/old/new/'
> '\$\_ .= \".ext\"'
> 'tr[A-Z][a-z]'\n";

You should use a here doc so you don't have to backslash everything:

my $op = shift or die <<'USAGE';
Usage: rename perlexpr [filenames]
where perlexpr is any perl expression that modifies $_:
    's/old/new/'
    '$_ .= ".ext"'
    'tr/A-Z/a-z/'
USAGE


> if( !@ARGV ) {
> @ARGV = <STDIN>;
> chop( @ARGV );

You should use chomp instaed of chop:

     chomp( @ARGV = <STDIN> );


> }
> for( @ARGV ) {
> $was = $_;
> eval $op;
> die $@ if $@;
> rename($was,$_) unless $was eq $_;
> }

That looks a lot like Larry's rename script which is distributed with 
some versions of Linux.



John
-- 
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About