For some general solutions to renaming files, see Larry's historic
rename script:
http://www.csit.fsu.edu/~burkardt/pl_src/rename/rename.html
Aristotle's modern facelift of that script at:
http://www.perlmonks.org/?node_id=303814
and the Unix rename faq:
http://www.faqs.org/faqs/unix-faq/faq/part2/section-6.html
Renaming files is also discussed in Perl Cookbook, recipe 9.9.
I think the begi...@perl.org mailing list is more appropriate
for this type of question.
HTH,
/-\
____________________________________________________
On Yahoo!7
Messenger - Make free PC-to-PC calls to your friends overseas.
http://au.messenger.yahoo.com
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/f...@perl.org/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,$_
Yep. To aid sight in this case, Deparse is your friend:
# perl -MO=Deparse f.pl
BEGIN { $/ = "\n"; $\ = "\n"; }
LINE: while (defined($_ = <ARGV>)) {
chomp $_;
$o = $_;
tr/ /_/;
print "renaming $o ===> $_" and rename $o, $_ unless -e $_;
}
f.pl syntax OK
Cheers,
To handle lots of extensions without having to edit your code, I'd suggest
using a dispatch table.
# initialise the table
my %ext = (
mpeg => 'mpg',
ram => 'rm',
qt => 'mov',
jpeg => 'jpg',
);
# build up a single regexp
my $ext_re = qr/\.(@{[join'|', sort keys %ext]})$/;
and in your function:
# fix odd extensions
$new =~ s/$ext_re/$ext{$1}/;
When your list of extensions grows, you only have to update the table.
And you can imagine a script that reads the table content from a
configuration file, which gives you even more flexibility.
By the way, golfers, do we have a name for the secret circumfix
@{[]} operator?
--
Philippe "BooK" Bruhat
Even the most powerful being is at the mercy of the weakest.
(Moral from Groo The Wanderer #34 (Epic))
> By the way, golfers, do we have a name for the secret circumfix
> @{[]} operator?
A derefarrance? (deref-array-nce) ?
> Even the most powerful being is at the mercy of the weakest.
> (Moral from Groo The Wanderer #34 (Epic))
From Groo? Surely this should read "most clever being is at the mercy
of the stupidest"?
--
Jasper