in reply to $|++ Does What, Exactly? (was: $++ Does What, Exactly?)

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