|
想要通过命令行参数传一个目录
然后把每个文件名和mtime信息存入哈希表里
可是mtime总是读不到
下面是程序:
#!/usr/bin/perl -w
use strict;
#use warnings;
my $directory ;
my $num=@ARGV;
if ($num ==0) {die "please input the directory through the command line !\n";
}
else { $directory = shift(@ARGV);}
if (!-e $directory) {
print "the directory is not exist !\n";
exit;
}
print "-------start monitoring $directory -----(press ctrl+c to halt)--------\n";
my %file; #储存所有(文件名,mtime)
my $size;
#while(1){
&getstat();
sub getstat{
opendir DIR,$directory or die "cannot open the directory!\n";
my %file=();
my @files= readdir DIR;
print "@files\n";
foreach my $i (@files) {
my @stat= stat $i;
print "@stat\n";
my $mtime= $stat[9];
print $mtime;
$size= $stat[7];
$file{$i}= $stat[9];
}
#return %file;
foreach my $key (sort keys %file){
my $value = $file{$key};
print "$key => $value\n";}
}
#}
为什么只能读到. 和..的stat信息
其余的文件都为空?? |
|