print "OUT 1\n";
print STDERR "ERR 2\n";
print "OUT 3\n";
print STDERR "ERR 4\n";
print "OUT 5\n";
Run the script, piping all output into a file:
perl test.pl &> file.log
cat file.log
ERR 2
ERR 4
OUT 1
OUT 3
OUT 5
Output is in the wrong order :(
Run it again using unbuffer:
unbuffer perl test.pl &> file.log
cat file.log
OUT 1
ERR 2
OUT 3
ERR 4
OUT 5
Output is in the correct order :)