On Sun, 26 Feb 2012 20:30:56 +0100, Manfred Lotz wrote: > I want to run a shell command with the following constraints: > > a. I like to get the return code of the command b. Furthermore I want to > combine stdout and stderr so that the output comes in a natural sequence > like in the shell. c. I don't want to capture the output in a variable > (because the output could be really large and I don't want to wait for > the first line of output) > > I did this: > > use IPC::Open3; > > sub run_cmd { > my $cmd = shift @_; > > my $pid = open3(undef, *CMD_OUT, *CMD_OUT,$cmd); > > while ( <CMD_OUT>) { print "$_"; } > waitpid($pid,0); > my $rc = $? >>8; > return $rc; > } > > > I tested the code and it I can say it worked fine under fair weather > conditions. > > Question: Is the code ok, or could it be improved, or has it even flaws?
It doesn't have flaws. You could do it without the module with a piped open: sub run_cmd { my $cmd = shift; open my $fh, '-|', "$cmd 2>&1" or die "open: $!"; print while <$fh>; close $fh; return $? >> 8; } -- Peter Scott http://www.perlmedic.com/ http://www.perldebugged.com/ http://www.informit.com/store/product.aspx?isbn=0137001274 http://www.oreillyschool.com/certificates/perl-programming.php -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/