Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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;)

share|improve this answer
    
Thanks, it works as expected if I autoflush both STDERR and STDOUT. –  Will Sheppard Nov 26 '13 at 13:10
    
Will, please mark this answer as accepted. –  Luís de Sousa Nov 26 '13 at 13:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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