3

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

cat foo:

1
3
2
4

Why isn't the output in order, and how do I fix it?

5

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

STDOUT->autoflush(1);

(If you have an older perl, then the above may not work, instead you have to use something like $| = 1;)

  • Thanks, it works as expected if I autoflush both STDERR and STDOUT. – Will Sheppard Nov 26 '13 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.