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

Hi, I'm fairly new to Perl and I'm attempting to write a script that will parse a log file as is it being created. Basically, I've written a script that parses an already existing text file but I'm kind of lost on how to parse a file while another program has the file open and is adding text to it. Thanks
  • Comment on Parsing a text log file as it is being created

Replies are listed 'Best First'.
(z) Re: Parsing a text log file as it is being created
by zigdon (Deacon) on Dec 26, 2002 at 20:23 UTC
    I think what you're trying to do is use the functionallity of 'tail -f file' in perl. For that, I usually use seek. Here's a snipplet of how to use it:
    open (FILE,"newfile") or die "Can't read file: $!"; my $continue = 1; while ($continue) { while (<FILE>) { # do something with $_ if (/some_end_condition/) { $continue = 0; last; } } # when we get here, we're at the EOF seek (FILE, 0, 1); # reset the EOF flag sleep 1; # wait a bit }

    -- Dan

•Re: Parsing a text log file as it is being created
by merlyn (Sage) on Dec 26, 2002 at 21:47 UTC
      I downloaded File::Tail and tried inserting some code to use it but, I get an error stating that it can't find File/Tail in the @INC. I put the Tail.pm file in my c:\Perl\lib folder (which is in my path). Is that enough, or do I need to install this module some other way? I am using Activestate Perl 5.6.1 on WinXP Home. Thanks
        You need to put Tail.pm in C:\Perl\site\lib\File. Also you need to install Time::HiRes, easiest way is to use the Package Manager.
        PPM>install Time::HiRes

        poj