如何在 Perl 中异步监视文件?

发布于 2024-08-28 22:16:16 字数 262 浏览 3 评论 0原文

我想知道是否可能,如果可能的话,如何创建一个持续监视文件/数据库的 perl 脚本,然后在文件更改时调用子例程来执行文本处理。我很确定使用套接字可以做到这一点,但这需要用于在共享主机上运行的站点上的网络聊天应用程序,而且我不太确定它是否允许使用套接字。

基本思想是:

  • 为聊天文件/数据库创建一个侦听器,
  • 当文件更新为新消息时,调用子例程,
  • 被调用的子例程将把新消息发送回浏览器以进行显示,

提前致谢。

I am wondering if it is possible, and if so how, one could create a perl script that constantly monitors a file/db, and then call a subroutine to perform text processing if the file is changed. I'm pretty sure this would be possible using sockets, but this needs to be used for a webchat application on a site running on a shared host, and I'm not so sure sockets would be allowed on it.

The basic idea is:

  • create a listener for a chat file/database
  • when the file is updated with a new message, call a subroutine
  • the called subroutine will send the new message back to the browser to be displayed

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风蛊 2024-09-04 22:16:16

许多操作系统运行一项服务,允许应用程序注册请求,以便在文件或路径更新时收到通知。这通常称为文件更改监视器。有关某些可用系统,请参阅链接的维基百科页面。最近的linux系统使用Inotify,以前使用Dnotify或gamin。 OS X 使用 FSEvents。 Windows也有类似的系统。我不知道有任何模块或机制适用于所有这些系统的跨平台,但 CPAN 上有可用的特定模块,例如 SGI::FAMFile::Tail::FAM

Many operating systems run a service that allows applications to register a request to be notified when a file or path has been updated. This is generally called a File Alteration Monitor. See the linked wikipedia page for some systems available. Recent linux systems use Inotify, previously Dnotify or gamin were used. OS X uses FSEvents. Windows has a similar system. I don't know of any module or mechanism that works cross platform for all these systems, but there are specific modules available on CPAN, such as SGI::FAM and File::Tail::FAM.

后来的我们 2024-09-04 22:16:16

我将使用 cron 作业和调用 Perl 脚本的 Makefile 来完成此操作。方便的是,您可以自动获取 Perl 脚本的 atime 作为要比较的时间戳,因为脚本的 atime 在调用时会更新。

I'd do this with a cron job and a Makefile that invoked a Perl script. The handy thing is that you get the Perl script's atime automatically as the timestamp to compare against, since the script's atime is updated when it is invoked.

心房的律动 2024-09-04 22:16:16
use POE qw(Wheel::FollowTail);
POE::Session->create(
    inline_states => {
      _start => sub {
        $_[HEAP]{tailor} = POE::Wheel::FollowTail->new(
          Filename => "/var/log/thttpd.log",
          InputEvent => "got_log_line",
          ResetEvent => "got_log_rollover",
        );
      },
      got_log_line => sub {
        #print "Log: $_[ARG0]\n";
        parseline($_[ARG0]);
      },
      got_log_rollover => sub {
        #print "Log rolled over.\n";
      },
    }
  );

POE::Kernel->run();
exit;

#parseline()...etc.
use POE qw(Wheel::FollowTail);
POE::Session->create(
    inline_states => {
      _start => sub {
        $_[HEAP]{tailor} = POE::Wheel::FollowTail->new(
          Filename => "/var/log/thttpd.log",
          InputEvent => "got_log_line",
          ResetEvent => "got_log_rollover",
        );
      },
      got_log_line => sub {
        #print "Log: $_[ARG0]\n";
        parseline($_[ARG0]);
      },
      got_log_rollover => sub {
        #print "Log rolled over.\n";
      },
    }
  );

POE::Kernel->run();
exit;

#parseline()...etc.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文