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

I'm trying to convert a chat script I have from one server to another. On the original server, it works fine as printed. However, on the new one I'm having issues with it.

Here's a clip of the trouble-spot:
open (SOMEOUT,">&STDOUT"); print SOMEOUT "HTTP/1.0 200\n"; print SOMEOUT "Content-type: text/html\n\n"; print SOMEOUT "Text goes here\n"; print SOMEOUT "$line"; print SOMEOUT "<SCRIPT> scroll(); </SCRIPT> \n "; close (SOMEOUT); open (SOMEOUT,">&STDOUT");
Basically it repeats those last few lines for a while, each time a new line of input is written to the server, it reads it into the program, prints it to the output, and then waits for the next line of input to appear.

Here's the issue.
On the old server, when it got to the close command, it would display onscreen all the information it had, then open the STDOUT again to add a line to it, close it to display that line, etc. With the new one, it doesn't do any of that. It just sits there and displays absolutely nothing. There's a time-out built in (to prevent idle processes from eating CPU forever) so once that expires and the SOMEOUT is closed for good and the script ends, it displays onscreen everything that it was supposed to have been showing all along... so I know that it's getting all that information properly. It just refuses to build the screen as it goes.

Anybody have any idea why this would be? Any suggestions for how to fix it? Or, barring suggestions for that, does anybody have any good suggestions for other ways to create a streaming output to an HTML page that doesn't require constantly refreshing the page?

Thanks for any help you can give!

Mael

Replies are listed 'Best First'.
Re: Trouble with Streaming text to STDOUT
by robartes (Priest) on Oct 03, 2002 at 14:56 UTC

    This is a classic - you need to unbuffer your STDOUT. Normally, STDOUT is buffered. The long answer on how to do it is in the FAQ, the short answer is:

    $old=select(STDOUT); $|=1; select($old);

    CU
    Robartes-

      Thanks for the advise, but something still doesn't seem to be working.
      I tried reading over the FAQ entries that you pointed me at, and found the one that you're referring to. But I still can't make it work. I inserted the code you recommended (also found in the FAQ) into the top of my code as it's about to start the streaming output. From what I'd read that should be all I need, to set that to non-buffering for the rest of the program. But it still displays the same characteristics as before. Am I missing something else? Or did I put it in the wrong place, maybe?
      Any advice appreciated.

      Mael
        fglock's node below is pointing in the right direction for that issue. You should be unbuffering SOMEOUT, as you are printing to that filehandle, and not to STDOUT directly. Printing to STDOUT directly would have spared you this gotcha.

        $old=select(SOMEOUT); $|=1; select($old);

        CU
        Robartes-

Re: Trouble with Streaming text to STDOUT
by charnos (Friar) on Oct 03, 2002 at 14:57 UTC
    Have you tried setting $| (aka $OUTPUT_AUTOFLUSH) to a nonzero value? That should probably solve your problem, if it's set to a nonzero value, Perl will flush the output buffer every time a print statement is called. See perldoc perlvar for more info.
Re: Trouble with Streaming text to STDOUT
by fglock (Vicar) on Oct 03, 2002 at 19:19 UTC

    Have you tried plain  print without a filename? It should print to STDOUT by default.