Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a daemon on solaris 5.7 with perl 5.005 that processes log files in real time. I thought I'd use a tail -f so I could read the logs while they where being generated. I used an alarm to unblock the read and look around once in a while to see if i should finish reading. The problem is when its time to stop I can't close the file. The close gets blocked because the pipe on the other side isn't finished. Unfortunatly for me it never will so I need some way to cutting it off more forcebly. Does anyone have any ideas on how I can get this thing closed? Maby I need a better way of reading the file instead. Help would be very much appreciated.
open ENSTAIL, "tail +0 -f $ENS_LOG |"; while ( not $done ) { eval { alarm 60; $$line = <ENSTAIL>; alarm 0; }; if ($@) { if ( finishedReading() ) { close ENSTAIL; # BLOCKED! $done = 1; } } process( $line ); }

Replies are listed 'Best First'.
Re: blocked close filehandle
by merlyn (Sage) on Oct 16, 2000 at 00:58 UTC
      And look at how File::Tail implements the tail too. You'll learn a lot about perl from digging through this one. I have special love for this question because it was the one that sent me to c.l.p.m all those years ago. Imagine my surprise when I flip thru the group and spot authors of actual books that I own answering questions =)

      Luckily, someone else had already asked the question and been sent off to the Camel book. (look up seek). Once you've seen the cute little 5ish lines that it takes to do sketchy job of it, look in File::Tail at "sub position" and see how cool they are in handling all the cases and variations that we've never thought of.

      This module is the other poster child (e.g. CGI) for for people who think they can do it better and write it more "efficiently" themselves.

      Also, if your are going to use perl in the stream, it is often not worth it to use unix tools and pipe, once you are loading perl anyway, do the whole job in perl, you'll be happy later, promise.

      --
      $you = new YOU;
      honk() if $you->love(perl)

        These are great responses. Thanks!
RE: blocked close filehandle
by runrig (Abbot) on Oct 16, 2000 at 03:54 UTC
    This is one of those wheels I once reinvented, but I think it was before File::Tail came out. At least I read the perldocs. Just 'perldoc -q tail'.

    My 'wheel' is here if anyone's interested (look for upd_stats). It also includes an implementation of tac, which also is now sort of redundant (see File::ReadBackwards). Upd_stats is mainly a bundle of Informix related utilities, but also included some of these other things I had at the time.