#!/opt/perl_5.18.2/linux50/bin/perl
use strict;
#use warnings;
use File::ChangeNotify;
$| = 1;
my $watcher = File::ChangeNotify->instantiate_watcher(
directories => [ '/var/icc_shantesh/logs' ],
filter => qr/\.log/,
);
while ( my @events = $watcher->wait_for_events ) {
print "loop got executed\n";
print "File name: $_\n";
}
我在R& D环境中的上述代码中使用了File::ChangeNotify
。该脚本监视文件夹,并在发生更改时通知。我想知道已更改的文件的名称及其更新的内容
答案 0 :(得分:4)
@events
数组包含您想要的信息:
while (my @events = $watcher->wait_for_events){
print "loop got executed\n";
foreach my $event (@events) {
print "File name: " . $event->path . "\n";
}
}
该数组包含File::ChangeNotify::Event个对象,它们具有path
文件名访问器,type
表示已进行了哪些更改。
您可以查看手册页上的示例,并将代码重写为:
for my $event ( $watcher->new_events() ) {
print "File name: ", $event->path(), ' - ', $event->type(), "\n";
}
$_
是perl的默认变量,但是当您在循环中显式设置变量时(my @events = ...
)$_
未被使用。