I want to run a make command from a Perl script so I can capture its stdout and stderr streams. I know I could use open(MAKE, "make 2>&1 |") but this gives problems building the right shell command to pass arguments to make, and using open(MAKE, "-|", @makecmd, "2>&1") doesn't work because passing the command as an array doesn't spawn a subshell to do the redirection.
I came across IPC::Run3 and I've got it working, but my use of filehandles is ugly - basically I've had to spawn a cat subprocess to get a handle that I can tell IPC::Run3 to write to so my script can read from it, and my attempts at passing STDIN for this purpose have failed. What am I doing wrong?
#!/usr/bin/perl
use strict;
use warnings;
use IPC::Run3;
#my $pipe = \*STDIN; #-- this produces no output and hangs
#open(my $pipe, "<&STDIN"); #-- this outputs "foo bar" and hangs
open(my $pipe, "|cat"); #-- this works, but extra process is ugly
run3 "echo foo; echo bar >&2", \undef, $pipe, $pipe;
while (<$pipe>) {
print ">>> $_";
}