I thought of that, but the problem with tell+seek is that you can't go backwards unless you keep track of all the previous positions. A hybrid solution would do the trick.
my $max_lines = 100; open(my $fh, '<', $path) or die("Unable to open file $path: $!\n"); my $line = $cgi->param('line') || 0; my $pos = $cgi->param('pos'); if (defined($pos)) { seek($fh, $pos, 0) or die("Unable to seek to $pos: $!\n"); } else { if ($line) { do { <$fh> } while $. < $line; } } my $lines = $max_lines; print(qq{<div class="lines">\n}); while ($lines-- && defined(my $line = <$fh>)) { chomp($line); print(html_escape($line), qq{<br>\n}); } print(qq{</div>\n}); my $first = ($line == 0); my $last = not defined(<$fh>); my $prev_line = $line - $max_lines; $prev_line = 0 if $prev_line < 0; my $prev = "?line=$prev_line"; my $next_line = $line + ($max_lines - $lines) - 1; my $next_pos = tell($fh); my $next = "?line=$next_line&pos=$next_pos"; if ($first && $last) { print(qq{No other pages.<br>\n}); } else { print(qq{<a href="$prev">[prev page]</a> }) if !$first; print(qq{<a href="$next">[next page]</a> }) if !$last; print(qq{<br>\n}); }

In reply to Re^2: Reading Text Lines by ikegami
in thread Reading Text Lines 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.