In Perl, how can I watch a directory for changes?

后端 未结 2 742
清酒与你
清酒与你 2021-01-15 20:49
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`;
          


        
2条回答
  •  情歌与酒
    2021-01-15 21:29

    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.

提交回复
热议问题