0

当运行下面的测试代码来观察文件时,只有当我对文件执行“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 变量,但它们的结果都是一样的。

4

1 回答 1

4

我认为第二个参数Linux::Inotify2::watch是数字/位掩码,而不是字符串。你应该打电话

$inotify->watch($readfile, IN_ALL_EVENTS)

代替

$inotify->watch($readfile, "IN_ALL_EVENTS")

裸词IN_ALL_EVENTS被解析为(可能是常量)函数&Linux::Inotify2::IN_ALL_EVENTS

于 2012-05-04T16:21:16.867 回答