0

I've just started using Perl 5.26 on secure-CRT and i wrote a Perl script that capture calls multiple Perl scripts.

 my @secondCommand = capture("perl clientquery.pl -r $cid -l test.log -is $sqlFile");

I was wondering how can I capture the exit status of each capture calling and if it fails how can i make the original script die.

  • 1
    Where does the `capture()` function come from? Maybe it already returns the exit status somewhere? Or maybe it sets `$?` ? – Corion Oct 17 '18 at 11:12
  • what do you exacltly mean? can you be more specific? – Mohammed Dirir Oct 17 '18 at 11:47
  • use IPC::System::Simple qw(system capture); – Mohammed Dirir Oct 17 '18 at 11:47
  • capture from IPC::System::Simple will already die by default if the command fails or returns a nonzero exit status (this is the "simple" part). You can specify that it not die on certain exit codes (or any exit code), see [the docs](https://metacpan.org/pod/IPC::System::Simple#Exit-values). – Grinnz Oct 17 '18 at 17:47

1 Answers1

2

IPC::System::Simple provides $EXITVAL, which captures the exit code of commands run via capture and the other functions.

The exit value of any command executed by IPC::System::Simple can always be retrieved from the $IPC::System::Simple::EXITVAL variable:

This is particularly useful when inspecting results from capture, which returns the captured text from the command.

use IPC::System::Simple qw(capture $EXITVAL EXIT_ANY);

my @enemies_defeated = capture(EXIT_ANY, "defeat_evil", "/dev/mordor");

print "Program exited with value $EXITVAL\n";

$EXITVAL will be set to -1 if the command did not exit normally (eg, being terminated by a signal) or did not start. In this situation an exception will also be thrown.

simbabque
  • 53,749
  • 8
  • 73
  • 136