in reply to Sending html content to the browser as the script runs

Yes, as ikegami says, what you need (if you're not going the AJAX route) is an NPH script (stands for "No Parse Head"). I took a look at the documentation pointed to in the link above (to the NPH scripts section of the CGI.pm docs), and one of the things it doesn't mention is how and NPH script solves your problem. Most web servers will normally gather all the output together, attach headers, and then send the headers and contents to the client. You don't see anything until all the data is produced by the CGI. With NPH, the web server doesn't take on that responsibility, and in that situation it serves up data as it gets it from the CGI. If your web page has content which can be displayed as-you-go, it does.

One thing to not miss in the docs. On some web servers, the script has to start with the string "nph-". It's most awkward, but on those servers there is no way around it.

  • Comment on Re: Sending html content to the browser as the script runs

Replies are listed 'Best First'.
Re^2: Sending html content to the browser as the script runs
by perljunkymunky (Initiate) on Aug 03, 2006 at 05:36 UTC
    Through my browser, it seems to dump to the client all at one time still at the end. Would you guys mind scanning my code real quick and see if you notice anything suspicious? Should I be putting more in the header()?
    thanks,
    PJM
    #!/usr/local/bin/perl #script: nph-test_script_jmj.cgi use strict; use CGI qw(:standard -nph); $| = 1; ################### MAIN ########## CGI->nph(1); my $cgi = CGI->new(); print $cgi->header(-nph=>1); html_1(); print "my second is: " . mytime() . "<br>\n"; sleep 3; print "my second is: " . mytime() . "<br>\n"; html_2(); ############################################ sub html_1 { print "\n"; print "processing file<br>\n"; } sub html_2 { print "\n"; print "done processing file<br>\n"; } sub mytime { my ($sec) = localtime; return $sec; }
      I ran your script as is and got:
      processing file my second is: 42 my second is: 45 done processing file
      What server software are you using? Not all servers support NPH scripts.

      jdtoronto

        Apache 1.3

        When you ran the script, did it print the first two lines and then hang for 3seconds before printing the third and fourth lines?

        For me, it just dumped it all at once.

        -josh