title: Printing the output to a file in perl
body: <p>I have been trying to find examples related to file handling, but I haven't found anything that will solve my problem. </p>
  
  <pre><code>    use strict;
      use warnings;
  
      my ($m, $b)    = @ARGV;
      my $count_args = $#ARGV + 1;
      my $times      = 2**$m;
  
      my $output = "main fucntion to be called???\n";
  
      open(OUTFILE, "&gt; output1.txt") || die "could not open output file";
  
      print OUTFILE "$output\n"; ## Notice, there is not a comma after the file handle and the string to be output
  
      close(OUTFILE);
  
      main();
  
      sub main {
        if ($m =~ /^\d+$/) {
          if ($b =~ /^and$/i) {
            func_and();
          }
          else {
            print " not supported\n";
          }
        }
        else {
          print " enter valid number of pins\n";
        }
      }
  
      sub func_and {
        my $bit;
        for (my $i = 0 ; $i &lt; $times ; $i++) {
          my $p = sprintf("%0${m}b", $i);
          print "$p\t";
          my @c = split('', $p);
          my $result = 100;
          foreach $bit (@c) {
            if ($result &lt; 100) {
              $result = $result &amp;&amp; $bit;
            }
            else {
              $result = $bit;
            }
          }
          print "result is $result \n";
        }
      }
  </code></pre>
  
  <p>This program prints an output if I provide the input as 2 and the output is printed on the screen.</p>
  
  <p>I want to change the file handle <code>STDOUT</code> of this program that is i want to print the output to output1.txt file</p>
  
  <p>Can you please point out the mistake I am making?
      -----></p>
  
  <h1>copy STDOUT to another filehandle</h1>
  
  <pre><code>open (my $STDOLD, '&gt;&amp;', STDOUT);
  
  # redirect STDOUT to log.txt
  
  open (STDOUT, '&gt;&gt;', "$ARGV[1]".".log");
  
  print main();
  
  # restore STDOUT
  open (STDOUT, '&gt;&amp;', $STDOLD);
  
  print " check .log file\n This should show in the terminal\n"; 
  </code></pre>
  
  <p>This works for me.. but in the end of the log file i have digit "1" printed i don't know    why..I believe it is the timestamp being printed. I need to remove it.. do u know why is it happening?</p>