in reply to Re: how to populate array with last 20 lines of file
in thread how to populate array with last 20 lines of file

I tried it.
use strict; use File::tail; my $file = File::Tail->new('C:/Users/tiang/Documents/Perl/daily.20170 +731142932.txt'); while(defined(my $line = $file->read)) { print "$line\n"; }
the file is only 8KB, but it just got stuck. nothing in progress..

Replies are listed 'Best First'.
Re^3: how to populate array with last 20 lines of file
by talexb (Chancellor) on Aug 08, 2017 at 18:05 UTC

    Yeah .. not surprising, since you tail the file once, and then stop.

    Why not read it, sleep for a minute, then read it again, as your original script tried to do?

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

      in original script, the purpose of sleep() is to compare if the size of file changes. And Tail still does not solve my problem of only populating an array with last few lines of file, so does Readbackwards.
Re^3: how to populate array with last 20 lines of file
by shadowsong (Pilgrim) on Aug 08, 2017 at 21:23 UTC

    Try it again, but like this:

    #!perl -slw use strict; use File::Tail; my $logfile = File::Tail->new( name => 'logfile.txt' # daily logfile ,maxinterval => 5 # poll logfile every 5 seconds ,interval => 3 # wait 3 seconds before initial 'tail' ,tail => 5 # give me last 5 lines ); print while(defined($_ = $logfile->read));

    Best Regards,
    Shadowsong

Re^3: how to populate array with last 20 lines of file
by poj (Abbot) on Aug 08, 2017 at 18:23 UTC
    the file is only 8KB

    Why not read the whole file then ?