title: in perl, how do we detect a segmentation fault in an external command
body: <p>Following is C code that is destined to crash:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  int main() {
      char *p = NULL;
      printf("Value at P: %c\n", *p);
      return 0;
  }
  </code></pre>
  
  <p>When I compile and run it (RH4 machine with gcc 4.5.2), it predictably gives a segmentation fault:</p>
  
  <pre><code>%  ./a.out
  Segmentation fault
  
  %  echo $status
  139
  </code></pre>
  
  <p>If I run it with Perl v5.8.5, this happens:</p>
  
  <pre><code>%  perl -e 'system("./a.out") and die "Status: $?"'
  Status: 11 at -e line 1.
  </code></pre>
  
  <p>The <a href="http://perldoc.perl.org/perlvar.html" rel="noreferrer">perlvar</a> documentation for <code>$?</code> says that </p>
  
  <blockquote>
    <p>Thus, the exit value of the subprocess is really <code>($?&gt;&gt; 8 )</code>, and <code>$? &amp;
    127</code> gives which signal, if any, the process died from, and <code>$? &amp; 128</code>
    reports whether there was a core dump.</p>
  </blockquote>
  
  <p><code>11 &gt;&gt; 8</code> is <code>0</code>, and <code>11 &amp; 127</code> is <code>11</code>. </p>
  
  <p>Why the different exit statuses? If we cannot depend on the exit status, what should be the way to detect segmentation fault in an external command? </p>