使用IO :: Event检测目录中的新文件

Sma*_*elf 3 perl

我正在尝试使用IO :: Event来检测何时将新文件添加到目录中.我是IO :: Event库的新手,想知道它是否可以轻松实现.

我尝试了下面的代码,看看我是否可以让它做一些没有运气的事情.当我试图用opendir而不是时,它崩溃了open.

我只是想看看这个图书馆能否为我提供我想要的东西.我不需要普通Perl的解决方案,因为我可以自己编写代码.我看这个的唯一原因是因为我想使用Proc :: JobQueue :: EventQueue.我可以使用Proc :: JobQueue编写解决方案,但认为这可能更清晰.

#!perl

use warnings;
use strict;
use IO::Event;

open my $dirhandle,'/some/path/here/';

my $event = IO::Event->new($dirhandle);

Event::loop();

close $dirhandle;

sub ie_input{
    print "ie_input called\n";
}

sub ie_read_ready{
    print "ie_read_ready called\n";
}

sub ie_werror{
    print "ie_werrory called\n";
}

sub ie_output{
    print "ie_output called\n";
}
Run Code Online (Sandbox Code Playgroud)

Ωme*_*ega 5

我建议你使用File :: ChangeNotifyFile :: ChangeNotify :: Watcher.

use File::ChangeNotify;

my $watcher =
    File::ChangeNotify->instantiate_watcher
        ( directories => [ '/my/path', '/my/other' ],
          regex       => qr/\.(?:pm|conf|yml)$/,
        );

if ( my @events = $watcher->new_events() ) { ... }

$watcher->watch($handler);
Run Code Online (Sandbox Code Playgroud)