以下のテスト コードを実行してファイルを監視する場合、イベントは、ファイルに対して「vim」を実行して書き出す (または書き込み終了) 場合にのみ検出されます。ファイルへの「エコー」の実行、または perl によるテキストの追加は検出されません。
test_inotify.pl:
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use Carp;
use IO::File;
use Linux::Inotify2;
$|++;
my $readfile = shift;
#my $action = "IN_CLOSE_WAIT";
#my $action = "IN_MODIFY";
#my $action = "IN_OPEN";
my $action = "IN_ALL_EVENTS";
unless ($readfile) { $readfile = "test.txt" };
my $inotify = Linux::Inotify2->new();
$inotify->watch($readfile, $action)
or die "Inotify watch on " . $readfile . "failed: $!\n";
while () {
my @events = $inotify->read();
unless (@events > 0) {
print "Inotify Read Error: $!\n";
exit;
};
foreach my $event (@events) {
print "Detected Event: " . $event->fullname . "\n";
};
};
test_fh_write.pl:
#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
use Carp;
use IO::File;
$|++;
my $readfile = shift;
unless ($readfile) { $readfile = "test.txt" };
my $readfh = IO::File->new( $readfile, ">>" ) or
#my $readfh = IO::File->new( $readfile, ">" ) or
die "Cannot open $readfile: $!";
$readfh->autoflush(1);
if ($readfh) {
print $readfh "test\n\n";
};
undef($readfh);
私は test_fh_write.pl と、次のような echo コマンドを試しました:「echo a >> test.txt」、「echo "test" >> test.txt」など。
「$ |」でも試しました 文字となし ($fh->autoflush(1) を使用しても) ですが、役に立ちません。test_inotify.pl で定義されている各 $action 変数を試してみましたが、結果はすべて同じです。