如何在 Perl 中异步监视文件?
我想知道是否可能,如果可能的话,如何创建一个持续监视文件/数据库的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
许多操作系统运行一项服务,允许应用程序注册请求,以便在文件或路径更新时收到通知。这通常称为文件更改监视器。有关某些可用系统,请参阅链接的维基百科页面。最近的linux系统使用Inotify,以前使用Dnotify或gamin。 OS X 使用 FSEvents。 Windows也有类似的系统。我不知道有任何模块或机制适用于所有这些系统的跨平台,但 CPAN 上有可用的特定模块,例如 SGI::FAM 和 File::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.
我将使用 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.