Is this a dream or a nightmare? (Was Re: XML)

David T. Grove pete at petes-place.com
Sun Oct 8 10:01:39 EDT 2000


On Sun, 8 Oct 2000 03:25:17 -0400, "Tim Peters"
<tim_one at email.msn.com> wrote:

>[David T. Grove]
>> die unless @d = map {"$basedir/$_"} grep {!-d && -w} readdir(dir);
>
>1. You skipped over the code to open the directory handle (dir).
>   Presumably you need a few more lines, like
>
>    $basedir = "/code";
>    die $! unless opendir(dir, $basedir);

It's not the complete program (otherwise die would be superfluous
anyway, because it would immediately return, and I wouldn't be using
the list in any way). This is just a task I commonly perform, usually
in a recursive function. When not in the directory, I'd need

!-d "$basedir/$_" && -w "$basedir/$_"

or I could map first

grep {!-d && -w} map {"$basedir/$_"} readdir(dir)

if you want a complete program:

perl -MFile::Slurp -e 'for(grep{!-d && -w} map{"./$_"}
read_dir("./")){print "$_\n"}'

Anyway, it's just an example of a higher-order thought. Lzh in
undernet #perl could give you some that will make your head spin, but
he's a bit off the deep end. I try to keep my thoughts readable.

>3. Inserting a forward slash is not platform-independent (think e.g.
>   Macs, or even Windows if the paths are to be passed on to a DOS
>   command).

It is between Unices and Win32, which are the only platforms I use. I
don't even know what macperl looks like, and I believe it's developed
by an independent group IIRC.

>Python (2.0) code for doing all that correctly is no more lines (it could be
>squashed into fewer but not profitably), but they are longer; OTOH, they do
>more, and don't change the current directory:
>
>import os, stat
>basedir = os.path.normpath("/code")
>paths = [os.path.join(basedir, f) for f in os.listdir(basedir)]
>d = [p for p in paths if os.path.isfile(p)
>                      if os.stat(p)[stat.ST_MODE] & 0222]

>directly is about as good as it gets.  OTOH, people are sometimes surprised
>by what "-w" means in Perl!  That you're not mucking with the mode bits

cgi programmers? (vb grunts gone web)

>directly there means Perl gets to surprise you by picking a meaning you may
>not have intended -- at least in Python you can see exactly what "writable"
>is testing.  Although, if your background isn't Unix, it's utterly

In Perl, what has surprised me most is how often it has worked when I
was _guessing_ at grammar when I was first learning Perl 5. Either I
made some miraculous guesses or Perl correctly figured out what I was
trying to say and did it right anyway.

Anyway, we're seriously tangented. I'm not here to advocate Perl. My
original intention was just to express my admiration of Python and
give serious warnings about getting involved with ActiveState. (If I'm
going to use Python, I'd prefer my elders be wise enough to eschew
Dick into neverneverland. I don't want to have to fight to defend
against the death of a second language at the hands of that company.)




More information about the Python-list mailing list