title: Perl: Check if shell has redirected file handle
body: <p>I want to test if my perl program was called like:</p>
  
  <pre><code>my_program.pl 3&gt;&amp;1
  my_program.pl 3&gt;/tmp/file
  my_program.pl 21&gt;my.fifo
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>my_program.pl
  </code></pre>
  
  <p>So I need a way in perl to test if a file descriptor by number is opened by the shell.</p>
  
  <p><strong>Background</strong></p>
  
  <p>This is intended as an possible extension of GNU Parallel. Currently GNU Parallel only make sure STDOUT and STDERR are not mixed, so this will mix up output:</p>
  
  <pre><code>parallel 'echo begin {} &gt;&amp;3; sleep 1; echo end &gt;&amp;3' ::: a b c 3&gt;/tmp/file
  </code></pre>
  
  <p>whereas this will not because STDERR is buffered by GNU Parallel:</p>
  
  <pre><code>parallel 'echo begin {} &gt;&amp;2; sleep 1; echo end &gt;&amp;2' ::: a b c 2&gt;/tmp/file
  </code></pre>
  
  <p>Now there is no need to spend resources on a file handle that is not opened. This will simply fail:</p>
  
  <pre><code>parallel 'echo begin {} &gt;&amp;3; sleep 1; echo end &gt;&amp;3' ::: a b c
  </code></pre>
  
  <p>So GNU Parallel should only buffer file descriptor 3 if that is actually being redirected outside GNU Parallel. But I do not know of a way to detect that in Perl.</p>
  
  <p>On GNU/Linux I can go to /proc/$$/fd and see what descriptors are opened, but I would prefer a solution that does not depend on /proc/*/fd.</p>