in reply to print sleep print

Just to elaborate, and fill in a little bit on what Masem and Beatnik said...

The print, sleep, print is occuring when you expect it too. However, output is buffered -- meaning that what get sent to output get held until the buffer gets flushed.

The $| = 1; line tells Perl to shut off its buffering; So everything that gets printed goes out immediately.

Replies are listed 'Best First'.
(tye)Re: print sleep print
by tye (Sage) on Jun 26, 2001 at 19:02 UTC
    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")
      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

      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