I have a perl script:
warn "1\n"; print "2\n"; warn "3\n"; print "4\n";
I pipe the output to a file:
perl script.pl &> foo
1 3 2 4
Why isn't the output in order, and how do I fix it?
STDERR is automatically flushed after printing a line, STDOUT is not. The STDOUT buffer is flushed only if it's full. To force autoflushing also on STDOUT use
(If you have an older perl, then the above may not work, instead you have to use something like