Re: How to stream output to html page?
by pc88mxer (Vicar) on Mar 01, 2008 at 17:59 UTC
|
Use something like this - it works for me (running Apache):
#!/usr/bin/perl
use CGI;
$| = 1;
my $q = new CGI;
print $q->header(-type => 'text/html');
print "<pre>\n";
print "Running date:\n";
system("date");
print "sleeping...\n"; sleep(10);
print "Running ls:\n";
system("ls");
print "</pre>\n";
Most http servers now will stream output from CGI scripts to clients. That wasn't always the case in the early years. | [reply] [d/l] |
|
OK, I'm stupid..my script worked fine after I put "$| = 1;" in it. What does that do??
| [reply] |
|
$| is also known as $OUTPUT_AUTOFLUSH (if you use English). According to the documentation, "If set to nonzero, forces a flush right away and after every write or print on the currently selected output channel." See perlvar for more.
| [reply] [d/l] [select] |
|
Re: How to stream output to html page?
by shmem (Chancellor) on Mar 01, 2008 at 19:21 UTC
|
Depending on the page encoding, you may need to output chunks of NUL chars to fill up the client's buffer.
Some browsers tend to wait until the input buffer is filled before doing any character conversions:
# run process 1...
print $output1;
print "\0" x 1024;
# run process 2...
print $output2;
print "\0" x 1024;
# run process 3...
print $output3;
print "\0" x 1024;
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
| [reply] [d/l] |
Re: How to stream output to html page?
by kyle (Abbot) on Mar 01, 2008 at 19:01 UTC
|
| [reply] |
Re: How to stream output to html page?
by akho (Hermit) on Mar 01, 2008 at 17:54 UTC
|
No, the webpage is sent to the browser as a whole. So you can't do this from the ccgi script itself.
You can, however, do something like this with JavaScript on the client side. (that is, periodically query the server and ask if it has new stuff) | [reply] |
|
#!/usr/bin/perl -wT
use strict;
use warnings 'FATAL' => 'all';
use CGI ':standard';
use 5.010000;
$|++;
print header;
print start_html('Dots');
say 'One dot per second.<br>';
for ( 1 .. 30 ) {
print '. ';
sleep 1;
}
print end_html;
Runs here.
Update: Removed dead link. | [reply] [d/l] |
|
| [reply] |
Re: How to stream output to html page?
by TOD (Friar) on Mar 02, 2008 at 05:14 UTC
|
| [reply] |
|
Note: On a simple setup (such as Apache/Windows/Strawberry Perl) if you don't add a to insert a newline as part of the print, then it won't flush.
| [reply] |