Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How to stream output to html page?

by stickman (Acolyte)
on Mar 01, 2008 at 17:27 UTC ( [id://671400]=perlquestion: print w/replies, xml ) Need Help??

stickman has asked for the wisdom of the Perl Monks concerning the following question:

Hi, using a cgi script I want to output the results of
several process as they finish to a web page. For
example say I have process1,process2,process3 and each
one needs to run before the next and I want to output
the result of each to a web page as it returns it's output.

ie:
<html>
run process1...
print output

run process2...
print output

run process3...
print output

</html>

Does this make sense?
Can anybody point me in the right direction?
tia..

Replies are listed 'Best First'.
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.

      OK, I'm stupid..my script worked fine after I put "$| = 1;" in it. What does that do??

        $| 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.

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}
Re: How to stream output to html page?
by kyle (Abbot) on Mar 01, 2008 at 19:01 UTC
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)
      Not true:
      #!/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.

        Does not run in lynx.

        This method requires a cooperating webserver, a cooperating browser, very simple formatting of the webpage and a certain degree of contempt towards standards and best practices.

Re: How to stream output to html page?
by TOD (Friar) on Mar 02, 2008 at 05:14 UTC
    there are several ways to satisfy your intentions. here are two of them:
    1. set $| to a true value. this will autoflush the output stream after each print statement.
    2. take a look at the htt protocol. there is e.g. the response code 206 "partial content", or the widely unknown response code 100 "continue", which both might be of interest for you.
    --------------------------------
    masses are the opiate for religion.
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://671400]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-25 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found