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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.