title: Getting STDOUT, STDERR, and response code from external *nix command in perl
body: <p>I want to execute an external command from within my Perl script, putting the output of both stdout and stderr into a <code>$variable</code> of my choice, and to get the command's exit code into the <code>$?</code> variable.</p>
  
  <p>I went through solutions in <a href="http://perldoc.perl.org/perlfaq8.html#Why-can%27t-I-get-the-output-of-a-command-with-system%28%29?" rel="noreferrer">perlfaq8</a> and their forums, but they're not working for me. The strange thing is that I <em>don't</em> get the output of sdterr in any case, as long as the exit code is correct.</p>
  
  <p>I'm using Perl version 5.8.8, on Red Hat Linux 5. </p>
  
  <p>Here's an example of what I'm trying:</p>
  
  <pre><code>my $cmd="less";
  my $out=`$cmd 2&gt;&amp;1`;
  </code></pre>
  
  <p>or</p>
  
  <pre><code>my $out=qx($cmd 2&gt;&amp;1);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>open(PIPE, "$cmd 2&gt;&amp;1|");
  </code></pre>
  
  <p>When the command runs successfully, I can capture stdout. </p>
  
  <p>I don't want to use additional capture modules. How can I capture the full results of the external command?</p>