Re: file renamer (i'm a newbie)
Andrew Savige <[email protected]> Sat, 25 Mar 2006 16:42:35 +1100 (EST)
| Newsgroups | gmane.comp.lang.perl.golf |
|---|---|
| Message-ID | <[email protected]> |
--- 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: http://www.perlmonks.org/?node_id=437032 To help you learn about Perl one-liners, there are plenty of links to choose from here: http://www.perlmonks.org/?node_id=502295 And, of course, the most unreadable collection of Perl one liners ever created: http://terje2.perlgolf.org/~golf-info/Book.html (which seems to be down -- or has it moved?). and cog's secret operator thread: http://www.mail-archive.com/[email protected]/msg03431.html 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; $o=$_; tr/ /_/ or next; -e and print("$_ <=== already exists"),next; print"renaming $o ===> $_"; rename$o,$_ As a one-liner, this may be adequate: #!/usr/bin/perl -nl $o=$_;y/ /_/;-e||print("renaming $o ===> $_")&&rename$o,$_ HTH, /-\ ____________________________________________________ On Yahoo!7 Messenger - Make free PC-to-PC calls to your friends overseas. http://au.messenger.yahoo.com