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

I can highly recommend the File::Tail module as part of a solution to this problem.

Alex / talexb / Toronto

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

  • Comment on Re: how to populate array with last 20 lines of file

Replies are listed 'Best First'.
Re^2: how to populate array with last 20 lines of file
by ytjPerl (Scribe) on Aug 08, 2017 at 17:58 UTC
    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..

      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.

      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

      the file is only 8KB

      Why not read the whole file then ?