我在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
文件时,脚本没有打印任何内容。
答案 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"}