http://qs321.pair.com?node_id=20590

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (input and output)

I have a loop which processes a very large list of items. For each item it processes, it prints a dot to stdout (the console). However, instead of watching my "line of dots" growing until the last item is done, all of the items get processed and then I see all the dots appearing at once. Is there an equivalent to C's flush() function for Perl ?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How to flush output to the console?
by Anonymous Monk on Jun 30, 2000 at 18:53 UTC

    Autoflush the buffer. This is directly from perlvar:

    $|
    If set to nonzero, forces a flush right away and after every write or print on the currently selected output channel. Default is 0 (regardless of whether the channel is actually buffered by the system or not; $| tells you only whether you've asked Perl explicitly to flush after each write). Note that STDOUT will typically be line buffered if output is to the terminal and block buffered otherwise. Setting this variable is useful primarily when you are outputting to a pipe, such as when you are running a Perl script under rsh and want to see the output as it's happening. This has no effect on input buffering. (Mnemonic: when you want your pipes to be piping hot.)
    So, try $|++ or $| = 1. Both will turn on autoflushing, but I tend to like $| = 1 better since it is less cryptic for novices.

      there is also less cryptic command:

      autoflush STDOUT 1;

      But I'm not completely sure whether autoflush is pretty much the same as $|

      tomas
Re: How to flush output to the console?
by Anonymous Monk on Dec 01, 2003 at 09:37 UTC

    Turn on autoflush in a BEGIN block:

    BEGIN { $| = 1 }
Re: How to flush output to the console?
by Anonymous Monk on Jun 30, 2000 at 18:10 UTC

    Turn on "autoflush":

    $|++;
    Put this just after your #!/.../perl line.

Re: Flushing the screen
by satchboost (Scribe) on Apr 19, 2001 at 02:10 UTC
    To further the question, I've got a script that will do a "use This::Module" for every module I have in an application. There are some 1400+ modules, so I want to have a print statement every 100 modules to see where the compilation is at. I tried doing $|=1;, but that didn't flush - I still get all 14 print statements at the very end. The line I'm using to print is print "Compiling File ##\n";. Any thoughts?

    Originally posted as a Categorized Answer.

Re: Flushing the screen
by Anonymous Monk on Mar 31, 2004 at 08:47 UTC
    Don't know why exactly, if you're doing all this at compilation time there might be some salient differences in the context.
    Try warn or otherwise printing to STDERR, which in my experience is always line buffered.

    Originally posted as a Categorized Answer.