How to sort a set of files by date in a directory?

hi there,
I have a directory which contents I can parse dynamically. I end up with a file list. I then want to display those files sorted by date, oldest files first. I have very very little PERL experience...Would anyone know how to do that ? Thanks in advance.

One idea, in Perl, might be to create a hash from the filelist, using the mtime (obtained via stat) as the key. Afterwards it's only a foreach $time (sort keys %hash)
Or, on the shell:

ls -tr1 | sed -e '1d'

what would this look like in PERL though ?

Something like this (for all the .pl files in the current directory):

perl -e 'print join "\n", map { split /\t/; $_ = $_[1]; } sort map { @s = stat; $_ = "$s[9]\t$_"; } <*.pl>;'

Though I'm sure there are a few Perl gurus around who could do this with even less code. That last join is only for pretty printing, if you want to do something with your list you can omit it.

ok this works fine from a Unix shell. But when I integrate this into my script, I get this compilation error: "Use of implicit split to @_ is deprecated at perl_test2.pm line 19.". My Perl book also says that using @_ in this context is now deprecated. Would you know the workaround here ?

Change the line so that it splits to an array and use that (Hint: it's in the leftmost map)

here's what I end up with:

my @temp_array;
my @s;
my @sortedlist = map {  
                    @temp_array = split /\t/; 
                    @temp_array = $temp_array[1];}
                 sort 
                 map { @s = stat; $_ = "$s[9]\t$_"; }
                 @filelist;

print "@sortedlist\n";

now, I'm getting this error "Use of uninitialized value in concatenation (.) or string at perl_test2.pm line 20.", line 20 being the "map { @s = stat; $_ = "$s[9]\t$"; }" statement. Looks like it's not liking $, right ?

my @temp_array;
my @s;
my @sortedlist = map {  
                    @temp_array = split /\t/; 
                    $_ = $temp_array[1];}
                 sort 
                 map { @s = stat; $_ = "$s[9]\t$_"; }
                 @filelist;

print "@sortedlist\n";

Nope. $_ is always initialized inside a map() (see perldoc -f map), as long as there are elements, and is always changeable since that's what it was originally intended for. I rather think that stat() can't find your file since it isn't in the current directory and you don't supply the complete path (see perldoc -f stat).

ok , here's the final solution

#open the directory
unless (opendir DH, $mydir) {print "Can't open directory $mydir: $!\n";  
return undef;}

#sort them by creation date, oldest file first
my @filelist = sort { -M "$mydir/$b" <=>  -M "$mydir/$a" } grep { -f "$mydir/$_" } readdir DH;
closedir DH;

if you want to reverse the order, replace $b with $a.

Even though my end solution is quite different from yours, thanks for all the help plud.