Skip navigation.
Your source for Perl tips, howto's, faq and tutorials
( categories: )

The idea is to load the contents of the file in an array and return the last element. Depending of the size of the array, there are two different methods to use.

If the file is small, it's simpler to read the entire contents of the file in memory:

#-- open the file
open TXT, "  
#-- load the file into an array
@rows = ;
#-- print last line
print $rows[-1];

If the file is too big and/or you want to minimize memory consumption, use the Tie:File module (this module represents a text file as a perl array, each element of the array corresponds to a record in the file):

use Fcntl;
use Tie::File;
#-- open the file in read-only mode if you won't be modifying the file
tie my @rows, 'Tie::File', 'file.txt', mode => O_RDONLY or die "error: $!\n";
#-- print last line
print $rows[-1];