Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Capture external program return value

by tokpela (Chaplain)
on May 02, 2010 at 04:05 UTC ( #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{}.


Comment on Re: Capture external program return value
Download Code
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
Node Status?
node history
Node Type: note [id://837974]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2014-07-23 22:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (154 votes), past polls