How can I asynchronously control a file in Perl?

I am wondering if this is possible, and if so, then you can create a perl script that constantly monitors the / db file and then calls a routine to execute text processing if the file is modified. I am sure that this will be possible using sockets, but it should be used for the webchat application on a site running on a shared host, and I'm not sure that sockets will be allowed on it.

Main idea:

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

Thanks in advance.

+7
file perl listener chat
source share
3 answers

Many operating systems run a service that allows applications to register a request for notification when a file or path is updated. This is usually called File Alteration Monitor . See the related wikipedia page for some available systems. In modern linux systems, Inotify is used, previously Dnotify or gamin were used. OS X uses FSEvents. Windows has a similar system. I do not know a single module or mechanism that works with a cross-platform for all these systems, but there are certain modules in CPAN, such as SGI :: FAM and File :: Tail :: FAM .

+8
source share

I would do this with a cron job and a Makefile that called the Perl script. The convenient thing is that you automatically get the Perl script atime as a timestamp for comparison, since the script atime parameter is updated when it is called.

+1
source share
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. 
+1
source share

All Articles