On Tue Jul 08 10:27:52 2008, KANE wrote:
Show quoted text> Neither IPC::Run nor IPC::Open3 can interleave STDOUT and STDERR. For
> short bursts of output
> from a program, ie this sample:
>
> my $max = shift || 4;
> for ( 1..$max ) {
> $_ % 2 ? print STDOUT $_ : print STDERR $_;
> }
>
> IPC::[Run|Open3] will first read all of STDOUT, then all of STDERR,
> meaning theoutput
> looks like 1 line on each, namely '13' on STDOUT and '24' on STDERR.
>
> It should have been 1, 2, 3, 4.
Show quoted text>
> It's unclear to me how to solve this at this time, but i'll mention it
> as a caveat.
Two comments
1) the caveat is displayed incorrectly. It shows output as being on
successive lines, meaning you'd have to output $_."\n" instead of just
$_ -- however, doing that gives interleaved output -- thus the problem
goes away.
Harder is if there are no LF's, and there, only in the test case shown,
can it be easily solved...telling perl to use all raw output.
but it is a special case because the program being run thoughtfully
provides a means to do so. The general case is not possible, since
there is nothing stopping a program from completely buffering all output
and only dumping it on exit... But if it is perl....
Show quoted text> (PERLIO=':raw' perl -e 'for (1..4) {
$_%2 ?print STDOUT $_:print STDERR $_ }')
(output:)
1234
See Also 'unbuffer (1), an 'expect' script that uses the 'post
processing feature' on output to tell the libs not to buffer them but
allow each line to be processed as it is released. However, that didn't
solve the stated problem. However, combined with the difference in
behavior with the "\n" added to each bit of output, I was reminded perl
does line-buffering by default, so looked into 'perlrun' to see how that
might be toggled off.
Note, one might want to only use :rawio on STDOUT/ERR, the above short &
simple example says to use it on all I/O.
Anyway, not a general solution, but that's not possible, but with perl,
you can tell it to use :rawio....