Re: Should a dirhandle be a filehandle-like iterator?

2007-04-14 Thread Yuval Kogman
Why bother, actually, when it can just be a lazy list... Opendir and
closedir are very oldschool, and can be retained for whatever
technical detail they are needed, but in most modern code I think
that:

for readdir($dir_name) { .say }

should work as well.

The act of opening a directory is something I never quite got...
Even a directory with millions of entries is still peanuts in todays
memory sizes, and if it does need to be iterated very carefully the
old variants can still be around. readdir() returning a list doesn't
have to be inefficient but it's easier to screw up with it and make
it bloat.

-- 
  Yuval Kogman [EMAIL PROTECTED]
http://nothingmuch.woobling.org  0xEBD27418



pgpNgghKAUJR3.pgp
Description: PGP signature


Re: Should a dirhandle be a filehandle-like iterator?

2007-04-14 Thread Steve Peters
On Fri, Apr 13, 2007 at 07:43:23PM -0500, brian d foy wrote:
 As I was playing around with dirhandles, I thought What if... (which
 is actualy sorta fun to do in Pugs, where Perl 5 has everything
 documented somewhere even if nobody has read it).
 
 My goal is modest: explain fewer things in the Llama. If dirhandles
 were like filehandles, there's a couple of pages of explanation I don't
 need to go through.
 
 Witness:
 
 I can iterate through the elements of a named array with [EMAIL PROTECTED]:
 
my @a =  1 2 3 4 5 ;
for [EMAIL PROTECTED] { .say }   # but not = 1 2 3 4 5  :(
 
 and I can read lines from a file:
 
for =$fh { .say }
 
 Should I be able to go through a directory handle that way too? A yes
 answer would be very pleasing :)
 
my $dh = doc.opendir;
for =$dh { .say }# doesn't work in pugs
 
 And, since we're using objects now, .closedir can really just be
 .close, right? 
 
 And, maybe this has been already done, but wrapping a lazy filter
 around anything that can return items. I'm not proposing this as a
 language feature, but if many things shared the same way of getting the
 next item, perhaps I could wrap it in a lazy map-ish thingy:
 
my $general_iterator = lazy_mappish_thingy( doc.opendir ); 
 
for =$general_iterator { .say }
 
$general_iterator.close;  # or .end, or .whatever
 
 That last part is definetely not Llama material, but maybe I'll at
 least hit the haystack.

One of the things done for Perl 5.10 is to make dirhandles be a little
bit more like filehandles.  On OS's that allow it, things like

stat DIRHANDLE
-X DIRHANDLE
chdir DIRHANDLE

all make sense and do what you'd think they'd do.  

Steve Peters
[EMAIL PROTECTED]


Re: Should a dirhandle be a filehandle-like iterator?

2007-04-14 Thread Smylers
Jonathan Lang writes:

 Also: why distinguish between open and opendir?  If the string is
 the name of a file, 'open' means open the file; if it is the name of
 a directory, 'open' means open the directory.

Many programs open a file from a name specified by the user.  Even if
Copenfile existed, many programmers would surely continue to use
Copen for this.

Users being able to trick such programs into opening a directory rather
than a file could be unpleasant.

Smylers