There are three commonly proposed methods to do this:
Read the entire file
@array = <FILE>;
$last_line = $array[$#array];
This involves reading the entire file before examining the last line, which can be costly in memory and performance for large files.
Seek to the end and backtrack
seek(FILE, -81, 2); # 81 bytes from the end, which should
+be enough
@lines = <FILE>;
$last_line = $lines[$#lines]; # similar to above, but we don't read
+much
Use File::ReadBackwards
tie *FILE, File::ReadBackwards 'input_file';
while (<FILE>) {
# line-by-line starting from the end
}
This latter method is supposed to be efficient and fast, so I imagine it uses some variation of the seek() method above. |