readdir on Unix returns the underlying raw directory order. Additions and deletions to the directory use and free-up slots. The first two entries
to any directory are always created as "dot" and "dotdot", and these entries are
never deleted under normal operation.
in reply to Is readdir ever deterministic?
However, if a directory entry for either of these
gets incorrectly deleted (through corruption, or using the perl -U option and letting the superuser
unlink it, for example), the next fsck run has to recreate the entry,
and it will simply add it. Oops, dot and dotdot are no longer the first two entries!
So, defensive programming mandates that you do not count on the slot order.
And there's no promise that dot and dotdot are the first two entries, because
Perl can't control that, and the underlying OS doesn't promise it either.
-- Randal L. Schwartz, Perl hacker