Perl inotify2 每次文件修改都会触发两次

spr*_*aff 4 linux perl inotify

我正在编写一个 Perl 脚本来监视文件的更改。

#!/usr/bin/perl
use strict;
use Linux::Inotify2;

my $inotify = new Linux::Inotify2 or die $!;
my $filename = "/tmp/foo";
my $counter = 0;

$inotify->watch (
    $filename,
    IN_MODIFY,
    sub {
        ++$counter;
        print "changed: $counter\n";
    }
) or die $!;

1 while $inotify->poll;
Run Code Online (Sandbox Code Playgroud)

如果我像这样测试它,每次 /tmp/foo 发生变化时,该处理程序都会被调用两次(增加 $counter 两次):

echo abc > /tmp/foo
Run Code Online (Sandbox Code Playgroud)

为什么?

Laj*_*res 5

> 首先截断文件(我认为这本身也是一个修改)。尝试使用>>。