develooper Front page | | Postings from March 2006

Re: file renamer (i'm a newbie)

Thread Previous | Thread Next
Andrew Savige
March 24, 2006 21:42
Re: file renamer (i'm a newbie)
Message ID:
--- Eric Waguespack wrote:
> I was asking how to shorten it up to a one-liner using various perl
> idioms...

OK, I was just trying to protect you from the madness that is Perl
golf. ;-) If you don't believe me, you can read about some of the
history of Perl golf here:

To help you learn about Perl one-liners, there are plenty of links
to choose from here:

And, of course, the most unreadable collection of Perl one liners
ever created:

(which seems to be down -- or has it moved?).

and cog's secret operator thread:

Oh, and apart from that, don't forget to read:

 perldoc perlrun

Now to shortening your rename program.

First, notice that there is a bug in your version in that
s/ /_/ only changes the first space (and so will not work
correctly with filenames containing multiple spaces).

Some straightforward shortening without changing the algorithm:

#!/usr/bin/perl -nl
-e or print("$_ <=== does not exist"),next;
tr/ /_/ or next;
-e and print("$_ <=== already exists"),next;
print"renaming $o ===> $_";

As a one-liner, this may be adequate:

#!/usr/bin/perl -nl
$o=$_;y/ /_/;-e||print("renaming $o ===> $_")&&rename$o,$_



On Yahoo!7 
Messenger - Make free PC-to-PC calls to your friends overseas. 

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About