在Perl中,我怎样才能看到一个目录的变化? [英] In Perl, how can I watch a directory for changes?

查看:167
本文介绍了在Perl中,我怎样才能看到一个目录的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用Text :: Diff; ($ count = 0; $ count< = 1000; $ count ++){
my $ data_dir =archive / oswiostat / oracleapps。* dat
;
my $ data_file =`ls -t $ data_dir |头-1;
while($ data_file){
print $ data_file;
打开(DAT,$ data_file)||死(无法打开文件$!);
$ stats1 =(stat $ data_file)[9];
printStats:\\\
;
@ raw_data =< DAT>;
close(DAT);
printStats1 is:$ stats1\\\
;
sleep(5);
if($ stats1!= $ stats2){
@diff = diff \ @ raw_data,$ data_file,{STYLE => 上下文};
$ stats2 = $ stats1;
}
print @diff ||死(没有看到任何更新$!);




$输出:

  $ perl client_socket.pl 
archive / oswiostat / oracleapps.localdomain_iostat_12.06.28.1500.dat
统计信息:
Stats1是:
在client_socket.pl第18行没有看到任何更新。

你能告诉我为什么数据缺失以及如何解决这个问题?

解决方案

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



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

原因这个修正是有点模糊的。没有 chomp() $ data_file 包含一个尾随换行符:some_filename\\\
的两个参数形式open() 忽略结尾文件名中的换行符,我不知道为什么 因为两个arg打开模仿shell的行为。然而,你对 stat()的调用不会忽略文件名中的换行符,所以它是 stat()一个不存在的文件,因此 $ stats1 undef


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 $!");
   }
}

Output:

$ 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.

Can you tell me why the stats are missing and how to fix it?

解决方案

Note that I'm answering your original question, why the stat() seemed to fail, rather than the newly edited question title, which asks something different.

This is the fix:

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

The reason this is the fix is a little murky. Without that chomp(), $data_file contains a trailing newline: "some_filename\n". The two argument form of open() ignores trailing newlines in filenames and I don't know why because two-arg open mimics shell behavior. Your call to stat(), however, does not ignore the newline in the filename, so it is stat()ing a non-existent file and thus $stats1 is undef.

这篇关于在Perl中,我怎样才能看到一个目录的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆