If it isn't clear from the code example what's going on, here's my attempt at an explination. Unix systems (most) buffer output. In other words print "foo" doesn't happen immediately; the system holds this output in temporary memory (a buffer) until either a) the buffer is full or b) the system isn't using the disk and it's a convient time to flush (empty) the buffer. The idea is to improve disk performance.

There are some scenarios where you want the output to not be buffered. For example, you have several processes writing to the same file and you don't want the output of one to clobber the output of another (the Apache web server is a good example).

Try this piece of code

for (1..10) { print "$_\r"; sleep(1); } print "\n"; $| = 1; for (1..10) { print "$_\r"; sleep(1); } print "\n";
You should get a long pause and then all of a sudden "10". Then on the next line you should get a steady count from 1 to 10. The reason is that the 1 - 10 is in the buffer and hasn't been flushed to the screen yet. When $| becomes set to 1, the buffer flushes and you get all the output so fast that all you see is the 10. On the next one, the buffer isn't used, so you see every number as it is printed.

/\/\averick


In reply to Re: $++ Does What, Exactly? by maverick
in thread $|++ Does What, Exactly? (was: $++ Does What, Exactly?) by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.