Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

A few days ago, someone posted a question about reading directories. To discard the current and parent directories that are returned by readdir, someone suggested to:

opendir(DIR, "/users/foo/"); @bar = readdir(DIR); closedir(DIR); for (1..2) {shift @bar;} #get rid of '.' and '..'

Notwithstanding the lack of error checking, and a dubious array hack better handled by splice, merlyn subsequently pointed out that "There is no promise that the first two elements returned from a readdir are dot and dotdot," and I nodded in agreement.

Then I reflected on the fact that I have been using Perl for nearly ten years; I have use Perl on a half a dozen Unix variants, on VMS, on MS-DOS and Win32. In all that time, grovelling through filesystems has always been a large component of my work and yet I cannot recall a single time when . and .. were returned in any but the first two positions. Maybe it happens once in a while for any script in production -- I'm talking about when I'm stepping through some development code I'm testing.

I heed the warning and know that this behaviour is not guaranteed, so I dutifully code:

while( defined( my $file = readdir(DIR) )) { next if $file eq '.' or $file eq '..'; munge($file); }

but maybe it would be more elegant to be able to write code that throws away the first two results returned by readdir and then have a while block that doesn't contain first next if ... check. What I mean is that it's a nearly-invariant test that could be hoisted out of the loop if readdir was a little more deterministic.

My questions are

  1. Does there exist a platform where readdir returns . and .. in other than the first two return values?
  2. Is readdir guaranteed to return . and .. as the first two return values on any platform (specifically *BSD, Linux, Solaris and Win32)? (or more specifically the filesystem in use, such as ext2, ffs, ntfs...)

I realise my assumption is based on the notion that I consider a directory stream to be a linear list. Walking down it is akin to accessing array elements. A hash-based directory stream would produce unordered results, but I'm not sure I've ever encountered one, at least as far as the visible behaviour from userspace is concerned.

g r i n d e r

In reply to Is readdir ever deterministic? by grinder

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (4)
    As of 2018-06-24 05:47 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.