The problem with filesystems is that files are treated as
streams of data, instead of linked lists of lines. If files
were linked lists, then insertion and deletion would be a
snap, as well as traversing the list to a certain line.
As it is, the fastest way is to read X bytes from the end of
the file, and check for the right-most \n character (that is
NOT at the end of the string).
sub last_line {
my ($fref, $r_len) = @_;
my ($last, $chomped) = ("", 0);
$r_len = -$r_len if $r_len > 0;
$r_len ||= -80;
seek $fref, $r_len, 2;
{
my ($buffer,$pos);
read $fref, $buffer, $r_len;
$last = $buffer . $last;
if (($pos = rindex($last,"\n",length($last)-2)) != -1) {
return substr($last,$pos);
}
seek $fref, $r_len * 2, 1;
redo;
}
}
$_="goto+F.print+chop;\n=yhpaj";F1:eval