Possible Duplicate:
Reg: Perl CGI script Autoupdate with New Data
A perl CGI script continuosly updating the time, instead of creating a time list
#!C:/perl/bin/perl.exe
use warnings;
use strict;
use CGI;
local $| = 1;
my $cgi = new CGI;
my $string = localtime();
print $cgi->header();
print $cgi->start_html("Welcome");
print $cgi->h1("$string");
print $cgi->end_html();
print $cgi->header();
print $cgi->start_html("Welcome");
print $cgi->h1("$string");
print $cgi->end_html();
Your question reveals a fundamental misunderstanding of how things work.
The sequence of events is:
- The web server is configured to run a specific program when a specific URL is requested.
- A browser makes an HTTP request asking for the resource.
- The server runs the program, and captures its standard output stream, and sends that back to the browser.
- The browser displays the content.
After the request/response cycle is completed, the transaction is over. The server does not know where exactly the content it sent went, whether it's being displayed in a window or was converted to ASCII art. It sends the content and its done with it (TCP keep-alives etc do not change this model).
Therefore, the statement "CGI script which keeps on flushing with current time in the same browser instead of printing a list of times" is devoid of meaning: The server cannot take back the output it has already sent.
Using JavaScript and XHR, you can update the contents of a certain element on a page with output from a CGI script.
However, now another fundamental question to which you don't seem to have paid any attention rears its head: What do you mean by current time?
Switch to PSGI.
my $app = sub {
my $env = shift;
return sub {
my $respond = shift;
my $writer = $respond->([200, ['Content-Type', 'multipart/x-mixed-replace; boundary=time']]);
while (1) {
$writer->write(
"--time\n" .
"Content-Type: text/plain\n\n" .
localtime . "\n"
);
sleep 1;
}
# will never arrive here, but
# install a signal handler and call this for cleanup
$writer->close;
};
};
Perhaps you want to provide a META Refresh header in your output so that the page auto-refreshes every second. But really I'd recommend using Javascript for this.