如何在Windows上使用File :: ChangeNotify?

时间:2014-01-15 19:27:57

标签: perl winapi

我在Windows系统上安装了File::ChangeNotify并尝试运行以下代码:

my $watcher =
     File::ChangeNotify->instantiate_watcher
         ( directories => [ 'C:\files' ],
             filter  => qr/\.txt$/
         );


 # # blocking
 while ( my @events = $watcher->wait_for_events() ) { print "new event"}

当我运行脚本并尝试创建新的.txt文件或修改.txt下的c:\files文件时,脚本没有打印任何内容。

1 个答案:

答案 0 :(得分:5)

如果我添加这一行,它适用于我(在Linux上):

$| = 1;

然后我看到new event

请参阅perldoc perlvar$|$OUTPUT_AUTOFLUSH

以下是完整的代码:

use warnings;
use strict;
use File::ChangeNotify;

$| = 1;

my $watcher =
     File::ChangeNotify->instantiate_watcher
         ( directories => [ 'C:\files' ],
             filter  => qr/\.txt$/
         );


 # # blocking
 while ( my @events = $watcher->wait_for_events() ) { print "new event"}

更新:正如cjm明确指出的那样,添加换行符可以替代$|

 while ( my @events = $watcher->wait_for_events() ) { print "new event\n"}