in reply to I think I'm misunderstanding $|

Works fine for me, until I add the browser to the equation.

The program I used:

#!/usr/bin/perl $| = 1; print <<'END_HTML'; Content-type: text/html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <head> <title>PAGE TITLE</title> </head> <body> <p>Start. END_HTML sleep(5); print <<'END_HTML'; <p>done. </BODY> </HTML> END_HTML

Running it from the prompt:

$ foo.cgi Content-type: text/html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <head> <title>PAGE TITLE</title> </head> <body> <p>Start. ...waits 5 secs... <p>done. </BODY> </HTML>

Running it from via the web server:

$ telnet www.example.com 80 Trying 11.22.33.44... Connected to www.example.com. Escape character is '^]'. GET /script.cgi HTTP/1.0 Host: www.example.com HTTP/1.1 200 OK Date: Mon, 17 Dec 2007 22:39:09 GMT Server: Apache/2.0.54 (Unix) PHP/4.4.7 mod_ssl/2.0.54 OpenSSL/0.9.7e m +od_fastcgi/2.4.2 DAV/2 SVN/1.4.2 Vary: Accept-Encoding Connection: close Content-Type: text/html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <head> <title>PAGE TITLE</title> </head> <body> <p>Start. ...waits 5 secs... <p>done. </BODY> </HTML> Connection closed by foreign host.

But when viewed in FireFox 2, it waits for the entire document to arrive before display it.

Replies are listed 'Best First'.
Re^2: I think I'm misunderstanding $|
by sigmazero13 (Novice) on Dec 17, 2007 at 22:57 UTC
    Hmm, that is very strange. I guess I'll have to look and see if there's a way to force the browser to output immediately.
      You're barking up the wrong tree, if you ask me. See merlyn's column on long-running CGI processes.
        Hmm, very interesting. Actually, part of what I'm doing is re-writing this code altogether. The code here is just the cleanup I've done to make the "current" code work a little better, but I've been rewriting it for a future release, and I've done exactly what that article says: Fork out to a new process to do the data, and then have the "results" page just refresh itself until the process is done. Since I'm not sure why the browsers are being buggy, I'll just leave it as-is for now, I guess, and just try to get my new version finished up.
Re^2: I think I'm misunderstanding $|
by shmem (Chancellor) on Dec 18, 2007 at 10:41 UTC
    But when viewed in FireFox 2, it waits for the entire document to arrive before display it.

    We had that before. See Re: Clever autoflush detail.

    --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^2: I think I'm misunderstanding $|
by quester (Vicar) on Dec 18, 2007 at 08:14 UTC
    I just tried this under Apache 1.3.34 (ancient I know, but it was handy) and Firefox 2.0.11, and it renders the text immediately. Even if I replace the sleep 5 with this:
    for (1..15) { print; sleep 1; }
    it will update the page once per second.

    You might check if you are using a web proxy. The proxy could accumulate text before forwarding it to the browser.

    One thing to try is to terminate the current HTML element with something like <br> or <p>. As ikegami mentioned, it is not easy for the browser to guess exactly when it is time to update the display, and a paragraph break might give it a better clue.

    If all else fails, you could use DHTML: give an html id attribute to an HTML element you want to update, and spit out snippets of Javascript to update the element attributes. I have done that where a table in the middle of the page needed to be updated as the script in back of the page ran. It's pretty to watch it run, but it's not at all pretty to write, especially if you have to support both IE and FireFox.