in reply to Capture external program return value

You cannot determine whether an error occurred or not based on whether $! is not because $! is meaningless if no error occurred.

eval won't help as it catches exceptions and backticks do not throw exceptions.

Backticks are documented to return undef on error, so what you need is

my $result = `blastall -i foo -o bar -p blastx -d baz`; die("Could not execute blastall: $!\n") if !defined($result);

Replies are listed 'Best First'.
Re^2: Capture external program return value
by JavaFan (Canon) on May 02, 2010 at 09:03 UTC
    Backticks are documented to return undef on error
    And to further clearify, that's errors regarding the execution of the given program. The OP also mentions getting the return value - a non-zero exit code means (by convention) that the executed program encountered an error. In that case, backticks will not return undef. However, even backticks set $?, so the return value can be found as $? >> 8.
      my $result = `blastall -i foo -o bar -p blastx -d baz`; die("Could not execute blastall: $!\n") if !defined($result); die("Died from signal ", ($? & 127), "\n") if $? & 127; die("Exited with error ", ($? >> 8), "\n") if $? >> 8;
        I think that could be simplified to
        use IPC::System::Simple qw( capture ); my $result = capture('blastall -i foo -o bar -p blastx -d baz');