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

In reply to Re: end of file! by japhy
in thread end of file! by vnpandey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.