I'm trying to test the Perl module IPC::Run3 but having difficulty in checking whether a command is failed or successful.
I know that IPC::Run3 issues an exit code if something is wrong with its arguments, but what about if the arguments are ok but the command does not exist? How can I test the following example?
Having a subroutine to call Run3
sub runRun3 {
my $cmd = shift;
my ($stdout, $stderr);
run3($cmd, \undef, \$stdout, \$stderr);
# if( $? == -1 ) {
if (! $stdout and ! $stderr) {
die "Something is wrong";
} else {
print "OK \n";
}
}
when command $cmds[0]
below is executed (the ls
command of *nix systems) it prints OK
as expected, but with command $cmds[1]
it just says No such file or directory at ./testrun3.pl line 18
.
With a test to the exit code I want it to print Something is wrong
instead.
#!/usr/bin/perl
use warnings;
use strict;
use IPC::Run3;
my @cmds = qw(ls silly);
runRun3($cmds[0]);
runRun3($cmds[1]);
Or what would be the best alternative to IPC::Run3 in cases like this? This is just an oversimplification of the process, but eventually I would like to capture STDERR and STDOUT for more complex situations.
Thanks.