Feed on

sometimes, if you put a print statement inside of a loop that runs really really quickly, you won’t see the output of your print statement until the program terminates. sometimes, you don’t even see the output at all. the solution to this problem is to “flush” the output buffer after each print statement; this can be performed in perl with the following command:


as has been pointed out by r. schwartz, i’ve misspoken; the above command causes print to flush the buffer preceding the next output.

Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

14 Responses to “flush perl’s print buffer”

  1. on 16 Feb 2006 at 12:31 pm Randal L. Schwartz

    Actually, you can also use “$| = 1″ to do that, and some would consider that a bit less obscure.

    Also, that doesn’t flush the buffer. It tells print to flush the buffer on the next output. I think you know that, but it might not be clear reading that.

  2. on 16 Feb 2006 at 2:50 pm Lawrence David

    thanks for the note randal – i’ve modified my original post accordingly.

  3. on 15 Dec 2007 at 4:06 pm Dylan Doxey

    You might also use the English module and then you can do:


  4. on 26 Jul 2008 at 2:12 pm sylvainulg

    thanks for the tip. It saved the day.

    ** handing treasure chest **
    ** got a Courage-Mushroom **

    ** feel free to use it anytime you’ve got the PhD blues **

  5. on 19 Aug 2008 at 9:51 pm Lawrence David

    sweet — thanks :)

  6. on 01 Oct 2008 at 12:08 pm Adolfo garcia

    Hello, I’m newcomer to PERL programming, this instruction “$|=1″ should be inside the loop or this is required one time before the loop.

    Thank you for your support!! :-)

  7. on 01 Oct 2008 at 1:33 pm Lawrence David

    Hi Adolfo,

    It goes inside of the loop.

    Good luck!

  8. on 12 Nov 2008 at 9:01 am merry

    Nope, it’s one time:

    $| = 1;
    print “=”;
    sleep 1;
    print “\n”;
    $| = 0;
    print “=”;
    sleep 1;

    The second line is printed “whole” after 10s, while the firsat one is printed one = each second.

    Also, printing “\n” flushes the buffer, which you can verify by modifying the last print to “=\n”.

  9. on 23 Jul 2009 at 6:29 pm Dan

    Your correction is not right, as merry points out.

    From “perldoc 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 really buffered by the system or not; $| tells you only whether you’ve asked Perl explicitly to flush after each write). STDOUT will typically be line buffered if output is to the terminal and block buffered otherwise. Setting this variable is useful pri- marily when you are outputting to a pipe or socket, such as when you are running a Perl program under rsh and want to see the output as it’s happening. This has no effect on input buffering. See “getc” in perlfunc for that. (Mnemonic: when you want your pipes to be piping hot.)


  10. on 23 Jul 2009 at 6:37 pm Dan

    Also (re: merry’s post), printing “\n” does not flush, unless $| is already set to non-zero.

  11. on 26 Jan 2010 at 5:38 pm syrex314

    Thanks for posting this!

    All I can say is: wow, that was obscure.

  12. on 12 Feb 2010 at 7:05 am ITtuition.com

    Sweet mangoes! Thanks for this tip.

  13. on 11 Feb 2011 at 3:18 pm Glenn

    Excellent. Helped me solve a frustrating problem. Thanks!

  14. [...] 参考:http://desk.stinkpot.org:8080/tricks/index.php/2006/02/flush-perls-print-buffer/ Like this:LikeBe the first to like this post. Comments RSS feed [...]

Did I get this wrong? Let me know!

Trackback URI | Comments RSS