我试图获取按修改日期排序的文件列表.我从
Sort Directory and list files based on date and time 修改了示例程序并尝试运行它.
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"; }
我使用Strawberry Perl版本5.12.1.0在我的Windows XP 32位机器上运行它.
Windows上的目录列表是:
输出是:
解决方法
在get_sorted_files中,$dir是一个glob,而不是目录名.也许你的意思是$path?
my %hash = map {$_ => (stat($_))[9]} map { "$path/$_" } # $path,not $dir grep { m/.*/i } readdir $dir;