readdir conflict problem?

D

Davy

Hi all,

I write a program to generate file list and directory list seperately
from a directory.
But I found the readdir run correctly only once. The second readdir
always read in a null list? why?

#-------------------------------------------------------
use strict;
use warnings;

use Data::Dumper;

my $path = "something";

opendir(DIR, $path);

#--- get directory list
my @dir_list = grep{-d "$path/$_"} readdir(DIR);
print $dir_list[1],"\n";
print Dumper(\@dir_list);

#--- get file list
my @file_list = grep{-f "$path/$_"} readdir(DIR);
print $file_list[1],"\n";
print Dumper(\@file_list);


closedir(DIR);
#------------------------------------------------------------------

Thanks!
Davy
 
D

David Squire

Davy said:
Hi all,

I write a program to generate file list and directory list seperately
from a directory.
But I found the readdir run correctly only once. The second readdir
always read in a null list? why?

What is wrong with the documentation?

----

perldoc -f readdir:

readdir DIRHANDLE

Returns the next directory entry for a directory opened by "opendir". If
used in list context, returns all the rest of the entries in the
directory. If there are no more entries, returns an undefined value in
scalar context or a null list in list context.

....

----

Your first readdir (see code below) is in list context, so it returns
everything. The second readdir thus returns a null list, as per the
documentation.

You need to either:

a) store the list you get from the first readdir in an array and grep
that (my choice)

or

b) close and open the directory again before the second readdir.

Please read the documentation for the functions you are using before
posting questions here.


DS
#-------------------------------------------------------
use strict;
use warnings;

use Data::Dumper;

my $path = "something";

opendir(DIR, $path);

#--- get directory list
my @dir_list = grep{-d "$path/$_"} readdir(DIR);
print $dir_list[1],"\n";
print Dumper(\@dir_list);

#--- get file list
my @file_list = grep{-f "$path/$_"} readdir(DIR);
print $file_list[1],"\n";
print Dumper(\@file_list);


closedir(DIR);
#------------------------------------------------------------------
 
A

anno4000

David Squire said:
What is wrong with the documentation?

----

perldoc -f readdir:

readdir DIRHANDLE

Returns the next directory entry for a directory opened by "opendir". If
used in list context, returns all the rest of the entries in the
directory. If there are no more entries, returns an undefined value in
scalar context or a null list in list context.

...

----

Your first readdir (see code below) is in list context, so it returns
everything. The second readdir thus returns a null list, as per the
documentation.

You need to either:

a) store the list you get from the first readdir in an array and grep
that (my choice)

or

b) close and open the directory again before the second readdir.

c) rewinddir(), which exists explicitly for the purpose of reading
a directory handle again from the beginning.

Anno
 
T

Tony Curtis

On 24 Aug 2006 09:03:54 GMT,
c) rewinddir(), which exists explicitly for the purpose
of reading a directory handle again from the beginning.

d) use readdir in scalar context in a loop, then you don't
need 2 passes, and you don't need to store all the
directory entries in memory.

hth
t
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

readdir: is there a way to reset cursor to beginning? 8
Sorting 3
Merge files 1
Help with Hash 2
Dumper 4
Very Sluggish Code 4
readdir and regex 2
readdir the directory name and file name separately? 2

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top