Opendir and readdir in PERL

Hi All,

I have a perl script which reads the files from a directory and processes them. I am using PERL opendir and readdir commands for getting the files in the order they have created.

But i see that its picking the files in random order.
Is there anyway to control the order?
Looks like this is server or perl version dependent. because I am using Unix-solaris5.8 / PERL 5.8 and it works fine in this OS.
Same script picks the files in random fashion in Unix-solaris5.8 / PERL 5.6 version.
Any idea what settings needs to be changed to achieve the file process order?
my code:
opendir( MYDIR, $Path ) or die ( "Cannot open dir ${Path}: $!" );
while( defined( my $file = readdir(MYDIR) ) )
{
Process files in order;
}
close MYDIR;

perl will read the files in whatever order they are stored on the system. It will not pick files in random order. If you want to read them in some specific order look into the "stat" function or file test operators.

the files are not getting picked up in random order, they are getting picked up in the order the system reads them in. There are no other possibilities besides stat or file test operators and sort that I know of if you are going to use perl.

And well, not just Perl.. Why don't you want to use stat or sort? It would be the most ethical thing to do.. as KevinADC says; The files are stored in the order your system reads them in.. You could only control this with stat or sort. And it wouldn't even really take much..

finally found somedetails on google. Please go through the below link:
http://forum.java.sun.com/thread.jspa?threadID=5183881&tstart=15

It says:
"sort order"? Readdir may return a specific order, but it is not "sorted".

Can you show an example of what you're getting? How are you calling readdir? What filesystem are you using? UFS, ZFS, or something else?

On UFS, readdir should return entries in "directory" order, based upon positioning within the directory. This is identical to what is returned from 'ls -f' within the directory. Without the -f, 'ls' itself is responsible for sorting.

ZFS uses hashing techniques rather than a linear list to maintain a directory. This improves performance in some situations, but means that the order returned is not necessarily the order that the files were created (but the order should be stable unless the directory is changed).

The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones

09 F9 11 02
9D 74 E3 5B
D8 41 56 C5
63 56 88 C0
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. -- Jamie ZawinskiDetavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...BIT COINS ANYONE

ZFS is a filesystem that comes with Solaris. That means that the file ordering that you see there is ZFS specific (i.e.) probably only runs on Solaris. Linux is beginning to get support for ZFS, but it isn't there yet. Also, what if the files in question are under a different filesystem. You can't convince the owner to reformat it as ZFS.

Hence, the best solution is to read the files into an array and then call the perl function sort() to sort the files according to the desired sort order. This will work regardless of the filesystem used.