in reply to Re: print sleep print
in thread print sleep print

The $| = 1; line tells Perl to shut off its buffering;

A bit of a nit, but Perl isn't doing the buffering and setting $|=1 doesn't turn off any buffering. C's stdio library is doing the buffering and $|=1 tells Perl to issue an fflush() after each statement that writes to that file handle (the output handle that was selected when $| was modified).

Some consequences of this are that STDERR is (at least usually) not buffered even though:

select(STDERR); print $|;
prints "0" by default. Also, STDOUT is usually "line buffered" when going to an interactive terminal (the buffer gets flushed by the C RTL whenever a newline is found) but is usually "block buffered" (a fixed-length buffer is used and data is only flushed to the file when that buffer fills) when redirected to a file.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: print sleep print
by Abigail (Deacon) on Jun 26, 2001 at 22:56 UTC
    Note that typically if STDOUT is connected to an interactive terminal, a read from STDIN causes the buffer to STDOUT to be flushed.
    #!/usr/bin/perl -w print ">>>> "; sleep 3; <>; __END__
    Only after 3 seconds, the ">>>> " appears.

    -- Abigail

Thanks
by Anonymous Monk on Jun 26, 2001 at 23:38 UTC
    I suspected that it had something to do with buffering.
    The  $| = 1; worked in my little example. I guess it'll work in the rest of my scripts, too.
    Thanks everybody for the help.
    nixter