title: How can I restore STDOUT after redirecting it to a file in a Perl script?
body: <p>Below is the part of my code, my code enters "if loop" with <code>$value =1</code> and output of the process <code>iperf.exe</code> is 
  getting into <code>my_output.txt</code>. As I am timing out the process after <code>alarm(20sec)</code> time, also wanted to capture the output of this process only.</p>
  
  <p>Then after I want to continue to the command prompt but I am not able to return to the command prompt.</p>
  
  <p>Not only this code itself does not PRINT on the command prompt, rather it is printing on the <code>my_output.txt</code> file</p>
  
  <p>(I am looping this <em>if block</em> through rest of my code)</p>
  
  <h3>output.txt</h3>
  
  <pre>inside value loop2
  ------------------------------------------------------------
  Server listening on UDP port 5001
  Receiving 1470 byte datagrams
  UDP buffer size: 8.00 KByte (default)
  ------------------------------------------------------------
  [160] local 10.232.62.151 port 5001 connected with 10.232.62.151 port 1505
  [ ID] Interval       Transfer     Bandwidth       Jitter   Lost/Total Datagrams
  [160]  0.0- 5.0 sec  2.14 MBytes  3.59 Mbits/sec  0.000 ms    0/ 1528 (0%)
  inside value loop3
  clue1
  clue2
  inside value loop4
  one iperf completed
  Transfer
  
  Transfer Starting: Intent { act=android.settings.APN_SETTINGS }
  
  ******AUTOMATION COMPLETED******</pre>
  
  <p>Looks like some problem with reinitializing the <code>STDOUT</code>.</p>
  
  <p>I tried to use <code>close(STDOUT);</code> but again it did not return to <code>STDOUT</code>.</p>
  
  <h3>Code</h3>
  
  <pre><code>if($value)
  {    
  my $file = 'my_output.txt';
  
  use Win32::Process;
  
  print"inside value loop\n";
  # redirect stdout to a file
  
  open STDOUT, '&gt;', $file
    or die "can't redirect STDOUT to &lt;$file&gt; $!";
  
  Win32::Process::Create(my $ProcessObj,
                         "iperf.exe",
                         "iperf.exe -u -s -p 5001",
                         0,
                         NORMAL_PRIORITY_CLASS,
                         ".") || die ErrorReport();
  
  $alarm_time = $IPERF_RUN_TIME+2; #20sec            
  
  print"inside value loop2\n";
  sleep $alarm_time;    
  
  $ProcessObj-&gt;Kill(0);
  
  sub ErrorReport{
      print Win32::FormatMessage( Win32::GetLastError() );
  }
  
  print"inside value loop3\n";
  print"clue1\n";
  #close(STDOUT);
  print"clue2\n";
  print"inside value loop4\n";
  print"one iperf completed\n";
  }
  
  my $data_file="my_output.txt";
  
  open(ROCK, $data_file)|| die("Could not open file!");
  @raw_data=&lt;ROCK&gt;; 
  
  @COUNT_PS =split(/ /,$raw_data[7]);
  my $LOOP_COUNT_PS_4 = $COUNT_PS[9];
  my $LOOP_COUNT_PS_5 = $COUNT_PS[10];
  
  print "$LOOP_COUNT_PS_4\n";
  print "$LOOP_COUNT_PS_5\n";
  
  my $tput_value = "$LOOP_COUNT_PS_4"." $LOOP_COUNT_PS_5";
  print "$tput_value";
  close(ROCK);
  print FH1 "\n $count    \| $tput_value \n";
  </code></pre>