I am trying to get the list of files sorted by modification date. I modified the sample program from Sort Directory and list files based on date and time and tried to run it.
sub get_sorted_files {
my $path = shift;
opendir my($dir), $path or die "can't opendir $path: $!";
my %hash = map {$_ => (stat($_))[9]}
map { "$dir$_" }
grep { m/.*/i }
readdir $dir;
closedir $dir;
return %hash;
}
my %files = get_sorted_files(".");
foreach my $keys (sort{$files{$a} <=> $files{$b}} keys %files) {
print "$keys\t", scalar localtime($files{$keys}), "\n";
}
I am running this on my Windows XP 32-bit machine using Strawberry Perl version 5.12.1.0.
The directory listing on Windows is:

The output is:

The output doesn’t make much sense to me. What is going wrong with this piece of code and how exactly is the foreach loop sorting the list of files?
In
get_sorted_files,$diris a glob, not the directory name. Perhaps you meant$path?