Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi,
I am executing an external CLI utilties using the command pipe. I want to get the status of the command and also the output(error/info).
I am using the following code.
sub cmd_pipe { my $cmd = shift; open CMD, "$cmd | " or die "Cannot exec $cmd, $!"; my @data = <CMD>; my $result = close(CMD); return($result, @data); }

Problem here is, it returns the status correctly. Output is not stored in @data. But it is displayed in the console.
Can anybody figure out the error in my code?.
Thanks

Replies are listed 'Best First'.
Re: Problem capturing the output of external command
by LanX (Saint) on Dec 05, 2008 at 10:00 UTC
      Thanks.
      It is working now.
Re: Problem capturing the output of external command
by moritz (Cardinal) on Dec 05, 2008 at 09:51 UTC
    A possible explanation is that $cmd doesn't print its output to STDOUT but instead to STDERR. If that's the case you might need IPC::Open3 instead of open, or some redirect magic of which I'm not aware right now.
Re: Problem capturing the output of external command
by Anonymous Monk on Dec 05, 2008 at 10:17 UTC
    Redirect your stderr to stdout