1:foreach (sort(<*>)) {
2: $a = $_;
3: s/ /_/g;
4: s/(%20)/_/ig;
5: s/(%25)//ig;
6: $_ = lc($_);
7: rename("$a", $_);
8:}
There are a number of comments that can be made about the code on each line.
- Sorting the results is a needless expense that you can do away with.
- Using $a (a package variable) should be shunned. You should use
a lexical (declared with my). You should also choose a name that
documents what it contains.
- s/ /_/g is better expressed as a single character
transliteration with tr/ /_/, but...
- Since the following line also maps something to _, you should fold the
two together into one regexp. Also, since nothing is done with the matched
text you can drop the parentheses, and since none of the characters are
case-sensitive, you can also drop the /i modifier.
- In a similar vein, you can drop the parentheses and the /i modifier.
- Were the above regular expressions in fact case-sensitive, you could
put this line before them, to get it executed first, then in any event
you wouldn't have to worry about case-sensitivity in the first place.
- There is no need to double quote a single scalar, that's just unnecessary
interpolation. The compiler optimises this away, but it's a good habit to learn.
Also, you're not checking the error return, so if something goes wrong (renaming
to a file that exists), you'll never be informed.
Wrapping all those comments up, we get something like this:
foreach my $old( <*> ) {
(my $new = lc $old) =~ s/ |%20/_/g;
$new =~ s/%25//g;
rename( $old, $new ) or warn "can't rename $old to $new: $!\n";
}
For more information, you might want to visit this node, which talks about
the same subject.
update: oops! reading petral's reply made me realise I made a spelling mistake. Argh.
Hope this helps!
--g r i n d e r