develooper Front page | perl.beginners | Postings from February 2012

run command from perl

Thread Next
From:
Manfred Lotz
Date:
February 26, 2012 11:32
Subject:
run command from perl
Message ID:
20120226203056.647df260@arcor.com
Hi,
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?



-- 
Thanks,
Manfred





Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About