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". |