I have this Perl script sitting in the cgi-bin folder of my Apache server:
#!/usr/bin/perl
use strict;
use warnings;
$| = 1;
print "Content-type: text/html\r\n\r\n";
print "Hello there!<br />\nJust testing .<br />\n";
my $top = 5;
foreach (1..$top) {
print "i = $_<br />\n";
sleep 1;
}
What I want to achieve here is a gradual update of the web page to show the user an updated status. However, what I'm actually getting is the entire output at once, after a delay of 5 secs.
Is there any way I can write a script that is able to continuously inform the user of its progress? I have a script that takes a long time to finish and I would like to be able to see its progress in real time rather than the whole script to finish.
I have also tried to set autoflush mode to off ($| = 0), but even that doesn't do any thing.
Randal Schwartz shows a better way in Watching long processes through CGI.
That said, lying about the content type will not help. The following script does exactly what you seem to want it to do on Windows XP with Apache 2.2 (in that it takes ten seconds for the last line of output to appear):
#!/usr/bin/perl
use strict;
use warnings;
use CGI qw(:cgi);
print header('text/plain');
$| = 1;
print "Job started\n";
for ( 1 .. 10 ) {
print "$_\n";
sleep 1;
}
Now, if you are sending HTML, you cannot avoid the fact that most browsers will wait until they what they believe is enough of the page to begin rendering.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With