How to find the last line when reading a file from a loop in perl

I am working on a log log analysis using Perl. I am reading the log file as follows:

open(LOGFILE, "$logFile") || die "Error opening log file $logFile\n";                           
while(<LOGFILE>) 
{   
  #Processing statements goes here.
}

Now in the handlers I want to check if the file read pointer is on the last line. if so, then you want to process the condition before exiting.

So, the question arises from the while loop, which is used to read the file line by line. How to find out if it is in the last line?

+3
source share
2 answers
my $last;
while(<MYFILE>) {
   $last = $_ if eof;
}

Although, if you do not do something with every other line, it is quite inefficient.

+10
source

, File::ReadBackwards. , .

( ):

#!/usr/bin/env perl
use strict;
use warnings;
use File::ReadBackwards;

my $fh = File::ReadBackwards->new( 'dir_walk.rb' )
    or die "Can't read 'dir_walk.rb': $!";

my $last_line = $fh->readline;

print $last_line;

: , , . , , , , , . ( , .)

+6

Source: https://habr.com/ru/post/1713428/


All Articles