in reply to Re^2: How to share streams between processes
in thread How to share streams between processes

If you need the output from both stdout and stderr via a single stream:

#main my $kid = open my $fh, "theExternalProcess 2>&1 |" or die $!; while( <$fh> ) { ## Read the output from theExternalProcess } ## You'll end up here when the kid goes away

If you need to keep stdout and stderr separate, then see IPC::Open3 or IPC::Run.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^4: How to share streams between processes
by rapide (Beadle) on Sep 06, 2008 at 10:58 UTC

    Thanks for that reply. I needed to keep them separate but managed in the end to find both a solution and the reason for me to have the trouble in the first place. Seems like I forgot to flush the stream which made it seem the processes did not work in parallel;

    <P> select((select(STDOUT), $|=1)[0]); <P> And the whole script that made it all work: <br> <br>$pid = open3(*CMD_IN, *CMD_OUT, *CMD_ERR, $path, @arguments); <br> close(CMD_IN); <br> <br> $selector = IO::Select->new(); <br> $selector->add(*CMD_ERR, *CMD_OUT); <br> <br> while(@ready = $selector->can_read) { <br> foreach my $fh (@ready) { <br> if(fileno($fh) == fileno(CMD_ERR)) { <br> my $line = scalar <CMD_ERR>; <br> if(defined $line) { <br> parseErrorStream($line); <br> } <br> } <br> else { <br> my $line2 = scalar <CMD_OUT>; <br> if(defined $line2 and $print eq 1) { <br> parseStandardStream($line2); <br> } <br> } <br> $selector->remove($fh) if eof($fh); <br> } <br> } <br> close(CMD_OUT); <br> close(CMD_ERR);