1
use Text::Diff;
for($count = 0; $count <= 1000; $count++){
   my $data_dir="archive/oswiostat/oracleapps.*dat";
   my $data_file= `ls -t $data_dir | head -1`;
   while($data_file){
      print $data_file;
      open (DAT,$data_file) || die("Could not open file! $!");
      $stats1 = (stat $data_file)[9];
      print "Stats: \n";
      @raw_data=<DAT>;
      close(DAT);
      print "Stats1 is :$stats1\n";
      sleep(5);
      if($stats1 != $stats2){
         @diff = diff \@raw_data, $data_file, { STYLE => "Context" };
         $stats2 = $stats1;
      }
      print @diff || die ("Didn't see any updates $!");
   }
}

输出:

$ perl client_socket.pl
archive/oswiostat/oracleapps.localdomain_iostat_12.06.28.1500.dat
Stats:
Stats1 is :
Didn't see any updates  at client_socket.pl line 18.

您能告诉我为什么缺少统计信息以及如何解决吗?

4

2 回答 2

14

真正的修复是File::ChangeNotifyFile::Monitor或类似的东西(例如,在 Windows 上,Win32::ChangeNotify)。

use File::ChangeNotify;

my $watcher = File::ChangeNotify->instantiate_watcher(
    directories => [ 'archive/oswiostat' ],
    filter => qr/\Aoracleapps[.].*dat\z/,
);

while (my @events = $watcher->wait_for_events) {
    # ...
}
于 2012-06-28T20:21:29.287 回答
2

请注意,我正在回答您最初的问题,为什么stat()似乎失败了,而不是新编辑的问题标题,它提出了不同的问题。

这是修复:

my $data_file= `ls -t $data_dir | head -1`;
chomp($data_file);

这是修复的原因有点模糊。没有那个chomp()$data_file包含一个尾随换行符: "some_filename\n". 两个参数形式open() 忽略文件名中的尾随换行符,我不知道为什么,因为两个参数 open 模仿 shell 行为。但是,您对 的调用stat()不会忽略文件名中的换行符,因此它是stat()一个不存在的文件,因此$stats1undef.

于 2012-06-28T19:59:04.077 回答