You should note that the number of the current line is kept track off because Perl increments a counter each time it reads a line. Perl does that by basically looking at each byte of the input and when it has found the end of the line (what's in $/), it has read a line, and the counter can be incremented.

But when you seek to a certain position, Perl does not look at the bytes before the position you seek to (otherwise, seeking wouldn't be fast), so it has no way of knowing which line it is. Don't forget that on many systems, including Unix and Windows systems, files are just streams of bytes. The OS has no concept of lines.

If you must know the line numbers, what you can do is first you read the entire file, line-by-line, keeping track on which byte each line starts. You then have an index. Given the index, you can binary search to find the line number, given a position you seeked to.

Abigail


In reply to Re: Back Navigation In A File by Abigail-II
in thread Back Navigation In A File by Anonymous Monk

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.