Article 3615 of comp.lang.perl:
Xref: feenix.metronet.com comp.lang.perl:3615
Newsgroups: comp.lang.perl
Path: feenix.metronet.com!news.utdallas.edu!corpgate!bnr.co.uk!pipex!unipalm!ian
From: [email protected] (Ian Phillipps)
Subject: Re: tail -f clone in perl?
Message-ID: <[email protected]>
Organization: Unipalm Ltd., 216 Cambridge Science Park, Cambridge CB4 4WA, UK
References: <[email protected]>
Date: Tue, 22 Jun 1993 14:24:50 GMT
Lines: 45

[email protected] (Wolf Paul) writes:

>I need to write a script which monitors several files and
>collects lines added to these files into a single output
>stream...

>Can I have a file open and attempt a read, and just try
>again later if I get EOF, or will that not work?

I found it neccessary to remember the size and seek there each time,
otherwise I got core dumps. Probably a problem in stdio.  Your milage
may vary.

>Any hints, or a working tail -f clone, would be appreciated.

I hacked this in a few minutes a while ago as a sort of challenge. It's
not as sophisticated as the real "ntail" (qv via archie) but does the
job. It would probably be better not to keep opening the files...

#!/usr/local/bin/perl

while($_=shift)
{
   if (-d $_ ) { push( @ARGV, <$_/*> ); next; }
   (warn "Can't read $_: $!\n", next ) unless -r;
   $length{$_} = -s _;
   push( @files, $_ );
}

while(1)
{
file:
   for $file (@files)
   {
   open( I, $file ) || next file;
   seek( I, $length{$file}, 0 );
   print while $_=<I>;
   $length{$file} = tell I;
   }
   sleep 1;
}
--
Ian Phillipps, Unipalm Ltd, 216 Science Park,           Phone +44 223 250100
Milton Road, Cambridge, CB4 4WA, England.               Phax  +44 223 250101
PIPEX Ltd. is a wholly-owned subsidiary of Unipalm Ltd. - phone 0223 250120