in reply to What order do my files come from readdir() in?

I can answer your question on two grounds, neither of which refer to the source code. Both are pretty easy:

The first is that Perl runs on more than 30 OSs. For perl to guarantee an order, it would have to sort the file names before returining them to me. How annoyed would I be if I opened a directory with 15,000 files in it and perl paused for 5 seconds while it sorted the list. And how should it sort it? I would be doubly annoyed if it sorted it by name and I wanted it by date.

The second reason calls on laziness: the perl programmers aren't going to waste any precious braintime programming a directory read function if there is another one available. And there is one supplied by the OS in Unix, it's a system function called readdir(). The way to get a system directory handle is with opendir(). There's a pattern here... the perl programmers just pass the call straight through to the OS. Reading the manual for Solaris, I see that it goes out of it's way to avoid mentioning sort order for files in a directory. The reason for this is that you could have over five different types of file system, each returning file lists in a different order(by date, by size, by last accessed, by weird b-tree sort, etc.). How pissed off would we be if Solaris paused for five seconds every time we opened a directory with 15,000 files in it?

Substitute Linux for Solaris above and you have the same story, except you could have maybe 20 filesystems returning files in different orders.

____________________
Jeremy
I didn't believe in evil until I dated it.

  • Comment on Re: What order do my files come from readdir() in?

Replies are listed 'Best First'.
Re: Re: What order do my files come from readdir() in?
by Cody Pendant (Prior) on Jun 20, 2002 at 08:25 UTC
    Thanks for your help everyone. I guess it might well be creation/modification order, yes, but the main point is that we don't know for sure and can't rely on it being consistent: the OS will return whatever it feels like.

    Am I right, by the way, in thinking that the <=> operator will sort by leading numerals if it finds any?
    --

    ($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;
      Looks like it

      ____________________
      Jeremy
      I didn't believe in evil until I dated it.

      It's still probably better to use 'cmp' since you're doing a string comparison. Right tool for the right job sort of thing. In Perl, taking things on faith can come back and bite you in the ass when you least expect it.