HOW-TO watch log files with perl/EV for a fixed amount of time (timeout)?

Marc Lehmann schmorp at
Fri May 21 03:16:38 CEST 2010

On Thu, May 20, 2010 at 05:54:05PM -0600, Lars Holowko <Lars.Holowko at> wrote:
> my $fn = "tail -f /var/log/messages";
> open my $fh, '-|', $fn or croak "Can't open $fn: $::OS_ERROR";

starting an external process per file is not quite low-overhead, but
certainly easy to implement...

> my $watcher = EV::io $fh, EV::READ, sub {
>       my ($watcher, $revents) = @_;
>       warn "yeah, /var/log/messages should now be readable without
> blocking!\n";

No, not at all - you confuse events and non-blocking I/O - the fact that
some data is available does not suddenly make I/O non-blocking. For
that, you need to set your file descriptor to nonblocking (e.g. with
AnyEvent::Util::fh_nonblocking or a platform-specific ioctl/fcntl).

However, note that:

>       print <$fh>;

Making the fd non-blockign doesn't mean that you get full lines - for that,
you have to do your own buffering.

Either you do that, or you use some wrapper, scuh as AnyEvent::Handle
(note, this is untested, but should get you going):

   my $hdl = new AnyEvent::Handle
      fh => $fh,
      on_error => sub { ... },
      on_read => sub {
         my ($hdl) = @_;

         $hdl->push_read (line => sub {
            my ($hdl, $line) = @_;
            say "some data: $line";

> ----------------------------------------------------------------------
> The information contained in this transmission may be confidential. Any disclosure, copying, or further distribution of confidential information is not permitted unless such privilege is explicitly granted in writing by Quantum. Quantum reserves the right to have electronic communications, including email and attachments, sent across its networks filtered through anti virus and spam software programs and retain such messages in order to comply with applicable data security and retention requirements. Quantum is not responsible for the proper and complete transmission of the substance of this communication or for any delay in its receipt.

You are sending potentially confidential info to a public mailinglist and
then telling everybody on the list (and people reading the archive) that
they need written permission by some random company? I don't think this is
very encouraging if you want feedback from other people.

                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_    
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      schmorp at
      -=====/_/_//_/\_,_/ /_/\_\

More information about the libev mailing list