Search code examples
perlunixexitsecure-crt

how to capture the exit status of capture calling another perl script within a perl script and if it fails it should die


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.


Solution

  • 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.