The line $|=1 turns off buffering on STDOUT. I always use that when debugging code. STDERR is unbuffered by default and STDOUT is buffered by default.

What that means is that when writing to STDOUT, you may have to write say 4Kbytes of data before it actually gets output to the screen or the hard disk. This is what you want for the program to run fast. However, that means that the unbuffered STDERR messages come out immediately and "out of order" with the STDOUT printout. In other words, it can happen that the error message appears first and then much later the stuff associated with the error appears.

There is a big performance penalty for using $|=1, especially to the HD. The disk system works most efficiently with "blocks". A typical set up will be 512 byte fundamental "writeable" units on the HD, these are grouped together 8 at a time, to make a 4K block. And that is what is used to write to the disk and what is tracked by the directory system.

$|=1 will force a write for every "print" statement instead of have this buffered up to some larger number that the I/O system would rather deal with. You will find that even a one bye text file takes a minimum size unit on the disk.

Update with a demo:

#!/usr/bin/perl use warnings; use strict; my $x = undef; print "asdwf\n"; print "n,zxncv\n"; print "$x\n"; =Prints Use of uninitialized value $x in concatenation (.) or string at C:\Pro +jects_Perl\demounbuffer.pl line 8. asdwf n,zxncv =cut $|=1; $x = undef; print "asdwf\n"; print "n,zxncv\n"; print "$x\n"; =Prints asdwf n,zxncv Use of uninitialized value $x in concatenation (.) or string at C:\Pro +jects_Perl\demounbuffer.pl line 20. =cut
In the first code, the error message comes out first although this is the third print statement!!! In the second code (the same code, but with $|=1;), the error comes out 3rd in the print order! That more realistically presents the time line order of what actually happened.

I also show another "Perl trick", I used the PerlDoc feature to insert some explanation into the code. This is normally used to insert documentation at the beginning of the code, but I "cheated".


In reply to Re^4: Replace commas with spaces between quotes, parsing CSV by Marshall
in thread Replace commas with spaces between quotes, parsing CSV by BigRedEO

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.