Moving files
Erik Price
eprice at ptc.com
Mon Jan 6 11:55:28 EST 2003
Michael O'Donnell wrote:
>> perl -e 'opendir DIR,".";@f = grep { ! /^\./ && -f $_ } readdir(DIR);\
>> map {($n = $_)=~ s/\s+/_/g; rename ($_, $n) } @f;'
>
>
> Heh. Just for fun I rot13'd that Perl hack and, for my money,
> it's just as readable... ;->
LOL
> crey -r 'bcraqve QVE,".";@s = terc { ! /^\./ && -s $_ } ernqqve(QVE);\
> znc {($a = $_)=~ f/\f+/_/t; eranzr ($_, $a) } @s;'
>
> On a more serious note, I (for one) would be grateful for
> a blow-by-blow analysis of what you've done, since I'm in
> trying to learn how to look at Perl code without my eyes
> automatically crossing...
<code>perl -e</code>
executes Perl accepting a script as a commandline argument.
<code>opendir DIR, ".";</code>
opens the directory from which the script was executed and assigns it to
the file handle named "DIR".
<code>@f = grep { !/^\./ && -f $_ } readdir(DIR);</code>
readdir(DIR) opens the directory and passes the results (the file names)
to the grep function. grep filters the results of an expression (I
think), that expression being anything that does not begin with a dot
AND is a file, and assigns those things to an array named "f". (In
Perl, the "@" symbol signifies that a variable is an array, and the "$_"
variable is "the default item [passed from the readdir() function].)
<code>map {($n = $_)=~ s/\s+/_/g; rename ($_, $n) }</code>
assign "the default item [passed from the previous grep function]" to
the variable "n" and then substitute all whitespace in that item for an
underscore, then rename the default item to the string contained in "n",
but do this for each item in the "f" array.
This particular one-liner is very readable when properly indented and
code-formatted, making use of few of the esoteric symbols that make
reading Perl scripts hard. I like it quite a bit.
Erik
More information about the gnhlug-discuss
mailing list