title: $EXITVAL not working with capture in perl
body: <p>I need to run a Perl script from a Perl script, save the output in a log file and display an error message if I get an error. I don't want the script to end, just print the message that a call had failed. I am using capture to do this but I am unable to print the error message conditionally.</p>
  <p>I referred to <a href="https://metacpan.org/pod/IPC::System::Simple#runx(),-systemx()-and-capturex()" rel="nofollow noreferrer">this link</a>, particularly this text:</p>
  <blockquote>
  <p>$EXITVAL The exit value of any command executed by IPC::System::Simple
  can always be retrieved from the $IPC::System::Simple::EXITVAL
  variable:</p>
  <p>This is particularly useful when inspecting results from capture,
  which returns the captured text from the command.</p>
  <p><em>use IPC::System::Simple qw(capture $EXITVAL EXIT_ANY);   my
  @enemies_defeated = capture(EXIT_ANY, &quot;defeat_evil&quot;, &quot;/dev/mordor&quot;);<br />
  print &quot;Program exited with value $EXITVAL\n&quot;;</em></p>
  <p>$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.</p>
  </blockquote>
  <p>This is my code:</p>
  <pre><code>use IPC::System::Simple qw(capture EXIT_ANY $EXITVAL);
  my $output = capture(EXIT_ANY, $tool_path);
  if($EXITVAL == -1){
          print &quot;CHECK_FLOW: Error! check. Check the log file mem_check.log\n&quot;;
  }
  else{
          print &quot;CHECK_FLOW: Completed check.\n&quot;;
  }
  my $filename = 'check.log';
  open(my $fh, '&gt;', $filename);
  print $fh &quot;$output\n&quot;;
  close $fh;
  </code></pre>
  <p>I inserted an error in the $tool_path purposely and used the debugger which shows the $EXITVAL to be 0 in both cases.</p>
  <p>Is there anything I am missing? Is there any other way to accomplish what I am trying to do?</p>