|Welcome to the Monastery|
Is readdir ever deterministic?by grinder (Bishop)
|on Sep 17, 2001 at 18:39 UTC||Need Help??|
grinder has asked for the
wisdom of the Perl Monks concerning the following question:
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:
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
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