Output to STDOUT is buffered. In perl, a LF forces the buffer to clear, so
it prints immediately. That's why the third version works the way it does.
The buffer also outputs when it gets full. That's why the first one does
what it does; it is printing fast enough to fill the buffer quickly.
Your second program is just enormously slow ... if you let it sit for
(maybe) 15 minutes, you should see some output, whenever it gets around to
filling the buffer.
There is a switch in perl for unbuffered output (well, sort of unbuffered).
Try putting the line "$| = 1 ; " before you start ptinting and see if that
changes the way number two operates.
This same behavior is often a problem in CGI programs written in perl.
At 03:03 PM 7/27/00 +0000, Dan wrote:
>Here's one that's driving me mad ... I'm trying to write a script and I want
>the output to continue on the same line, but when I add in anything that
>slows down the loop I can only get an output if I add a line feed.
>
>(RH 6.1 and perl-5.00503-6.)
>
>Any ideas?
>
>Dan.
>
>Three example are below to try and explain what I mean.
>
>-----------------------------------------------------------------------------
>
>#!/usr/bin/perl
>
>while () {
> print "hello world";
>}
>
>gives
>
>o worldhello worldhello worldhello worldhello worldhello worldhello
>worldhello worldhello worldhello worldhello worldhello worldhello worldhello
>worldhello worldhello worldhello worldhello worldhello worldhello worldhello
>worldhello worldhello worldhello worldhello worldhello wor
>
>------------------------------------------------------------------------------
>
>When I add something in that slows the loop down, a subroutine, or even a
>simple sleep command ...
>
>#!/usr/bin/perl
>
>while () {
> print "hello world";
> sleep 1;
>}
>
>I get nothing at all, the cursor just sits there.
>
>------------------------------------------------------------------------------
>
>Then I add a line feed ...
>
>#!/usr/bin/perl
>
>while () {
> print "hello world\n";
> sleep 1;
>}
>
>And get this ...
>
>hello world
>hello world
>hello world
>hello world
>hello world
>hello world
>hello world
>hello world
--
------------------------------------"Never tell me the odds!"---
Ray Olszewski -- Han Solo
Palo Alto, CA [EMAIL PROTECTED]
----------------------------------------------------------------
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [EMAIL PROTECTED]
Please read the FAQ at http://www.linux-learn.org/faqs