Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Capture external program return value

by tokpela (Chaplain)
on May 02, 2010 at 04:05 UTC ( [id://837974]=note: print w/replies, xml ) Need Help??


in reply to Capture external program return value

IPC::Run3 will allow you to capture the output as well as the return code from the program you are running.

Untested late night code ;-)

use strict; use warnings; use IPC::Run3; my ($in, $out, $err); my @cmd = qw{blastall -i foo -o bar -p blastx -d baz}; eval { run3 \@cmd, \$in, \$out, \$err); } if ($@) { die "[Error] Command encountered an error in IPC::Run3 [$@]\n"; } if ($?) { die "[Error] Command encountered an error [$?]\n"; }

From the docs...

run3 throws an exception if the wrapped system call returned -1 or anything went wrong with run3's processing of filehandles. Otherwise it returns true. It leaves $? intact for inspection of exit and wait status.

Note that a true return value from run3 doesn't mean that the command had a successful exit code. Hence you should always check $?.

Updated with Perlbotics and Hue-Bond suggested use of qw{}.

Replies are listed 'Best First'.
Re^2: Capture external program return value
by Hue-Bond (Priest) on May 02, 2010 at 08:23 UTC
    Untested late night code ;-)
    my @cmd = ('blastall -i foo -o bar -p blastx -d baz');

    On Sunday morning ;^), this would look better if spelled like this:

    my @cmd = qw{blastall -i foo -o bar -p blastx -d baz};

     

    Edit: removed unneeded parens.

    --
     David Serrano
     (Please treat my english text just like Perl code, i.e. feel free to notify me of any syntax, grammar, style and/or spelling errors. Thank you!).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://837974]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-16 05:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found