in reply to Re: Reading Text Lines
in thread Reading Text Lines

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}); }