The web (and CGIs) are stateless, so "taking out the printed lines each time" isn't as easy as you think.
A better option would be to abuse the magical $. variable, that tells you which line you're on when iterating over a file handle, so if your CGI submits an offset and number of lines to display, you can do something like...
print header;
my $start = param('start_line');
my $end = $start + param('show_lines') - 1;
if ( open TEXT, "< /path/to/your/file.txt" ) {
while ( <TEXT> ) {
last if $. >= $end;
print if $. >= $start;
}
} else {
print "Oops, there was a problem!";
}
See also HTML::Pager (alt.).
--k.
| [reply] [d/l] |
Repeatedly passing the lines back and forth is really going to be inefficient. Best thing to do would be to use tell after you've read the lines and pass that in a hidden field. When you get a request with that field set, use seek to reposition and read the next group.
| [reply] [d/l] [select] |
ok. i'll look into that solution. i was thinking every time the script runs it can just take 20 lines out of the array and store it in a separate array, then print the first, and pass the second. then each time it runs, the array will get smaller and smaller till it's finished. i would have to maintain state to do that? | [reply] |
yup. (unless you get into nasty complicated threads and ports and sockets and so forth). See, the web browser will hit the cgi, get the first 20 lines, the cgi will print the first 20, do something with the next 20, then die, the browser will then hit the cgi, get the next 20..
I would suggest (like above) (ab)using the magical $. var, but use it as a query string instead of a hidden field, blah.cgi?pos=20; as this is much easier to change, and advanced users can see whatever part they want..
| [reply] |
ok. i see your point. i guess i'm glad i didn't start building THAT script. | [reply] |