title: How to read STDIN and write content back to STDIN in Perl?
body: <p>I have to light patch some of the core components within an internal software product. Due to its realy nasty handling of STDIN I have to:</p>
  
  <ol>
  <li>read STDIN and backup RAW</li>
  <li>parse it</li>
  <li>write RAW back to STDIN</li>
  <li>let the downstream system handle STDIN</li>
  </ol>
  
  <p>Unfortunally I am not allowed to modify any of the legacy code so I have to light patch it.</p>
  
  <p>My first not working try:</p>
  
  <pre><code>BEGIN {
    my $stdin_raw = join '', &lt;STDIN&gt;;
    use IO::Handle;
    my $stdin_io1 = IO::Handle-&gt;new();
    $stdin_io1-&gt;printflush( $stdin_raw );
    my $stdin_io2 = IO::Handle-&gt;new();
    $stdin_io2-&gt;printflush( $stdin_raw );
    STDIN-&gt;fdopen( $stdin_io1, 'r' );
    require CGI;
    warn CGI::param('PARAM1');
    warn CGI::param('PARAM2');
    STDIN-&gt;fdopen( $stdin_io2, 'r' );
  }
  </code></pre>
  
  <p>It's able to read STDIN in the first line but nighter CGI nor the downstream system are getting any input from STDIN which I (tried) to set as listed above.</p>
  
  <p>So: <strong>How to read STDIN and write content back to STDIN in Perl?</strong></p>
  
  <p>The application is running under CGI (webserver) condition. STDIN handles the POST data of a web request.</p>