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?

Will Sheppard
  • 704
  • 7
  • 12

1 Answers1

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

Slaven Rezic
  • 165
  • 3