I created a child process via IPC::Open2
.
I need to read from the stdout of this child process line by line.
Problem is, as the stdout of the child process is not connected to a terminal, it's fully buffered and I can't read from it until the process terminates.
How can I flush the output of the child process without modifying its code ?
child process code
while (<STDIN>) {
print "Received : $_";
}
parent process code:
use IPC::Open2;
use Symbol;
my $in = gensym();
my $out = gensym();
my $pid = open2($out, $in, './child_process');
while (<STDIN>) {
print $in $_;
my $line = <$out>;
print "child said : $line";
}
When I run the code, it get stucks waiting the output of the child process.
However, if I run it with bc
the result is what I expect, I believe bc
must manually flush its output
note:
In the child process if I add $| = 1
at the beginning or STDOUT->flush()
after printing, the parent process can properly read from it.
However this is an example and I must handle programs that don't manually flush their output.