Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: readdir missing one file

by true (Pilgrim)
on Oct 17, 2008 at 16:00 UTC ( [id://717783]=note: print w/replies, xml ) Need Help??


in reply to Re: readdir missing one file
in thread readdir missing one file

hmm, i've been using this with no errors for almost a decade. Odd that it never was a problem before. but your are right. This code works:
opendir Tdir, "/var/www/campbell/campbell.edu/www/content/2/"; while(my $d = readdir(Tdir)){ print "<li>$d\n"; }close Tdir;
jtrue

Replies are listed 'Best First'.
Re^3: readdir missing one file
by rev_1318 (Chaplain) on Oct 17, 2008 at 16:25 UTC
    It was a problem before! That's why you never saw the '.' directory; you discarded it. Obviously, in the past you got the files from readdir ordered, with the '.' and '..' directories in front and that's not the case on RH (and shouldn't be depended on).

    Paul

      Exactly! Reading a file or directory alters the system by incrementing pointers: it's best to read only once, but if you read more than once, at least limit your reads to once place. This sort of thing is exactly why I started reading like this:
      my $path = "/path/to/whatever"; opendir (DIR, $path) or die "Can't read $path: $!"; my @files = grep { not /^\.{1,2}$/ } readdir (DIR); closedir (DIR); print join("\n", sort @files), "\n";
Re^3: readdir missing one file
by Illuminatus (Curate) on Oct 17, 2008 at 16:23 UTC
    Sounds like your new version of redhat has a subtle change to the readdir(2) operation (or maybe you changed the filesystem type you were on??). I am pretty sure that, in general, readdir simply returns the entries in the order that they appear in the file. You never used to see "." because it should always be the first entry in the directory file, and your script was discarding it. Now, for some reason, the first entry returned by readdir was not ".", but a file you cared about. Since opendir/readdir say nothing about entry ordering, "it's not a bug -- it's a feature":)

      Indeed I had never relied on that behaviour myself, but on quite about every filesystem I had seen thus far would readdir return qw/. ../ as the first two entries, and yet I had never ever seen this described as a requirement, so I had always wondered if by any chance it were - except undocumented or documented in some arcane place: now we have at least one bit of experimental evidence it actually isn't. (Of course I'm not claiming to be a fs expert or let alone knowledgeable person, in which case I would have known better to start with...)

      --
      If you can't understand the incipit, then please check the IPB Campaign.
Re^3: readdir missing one file
by ikegami (Patriarch) on Oct 17, 2008 at 17:05 UTC
    You still have two bugs and two instances of poor programming.
    • close doesn't work on directory handles. You need closedir .

    • You'll stop iterating prematurely if you have a file named 0 (zero). You need to check if the result of readdir is defined. Check if it's false isn't good enough.

    • Why are you using a global variable for the directory handle?

    • It's so easy to check opendir for errors, it's a very likely candidate for errors. Add a check!

    my $Tdir_qfn = "/var/www/campbell/campbell.edu/www/content/2/"; opendir(my $Tdir_dh, $Tdir_qfn) or die("Can't read dir \"$Tdir_qfn\": $!\n"); while (defined( my $f = readdir($Tdir_dh) )) { print "<li>$f\n"; } closedir($Tdir_dh);
      You'll stop iterating prematurely if you have a file named 0 (zero).
      That hasn't been true for a very long time:
      perl -MO=Deparse -e 'while ($f = readdir $h) {1;}' while (defined($f = readdir $h)) { '???'; } -e syntax OK

        I personally believe that I've just discovered the second little bit of C<while> (being more) magic (than I already knew) in just a few days...

        --
        If you can't understand the incipit, then please check the IPB Campaign.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://717783]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-28 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found