title: How to read from a file and direct output to a file if a file name is given in the command line, and printing to console if no argument given
body: <p>I made a file, "rootfile", that contains paths to certain files and the perl program mymd5.perl gets the md5sum for each file and prints it in a certain order. How do I redirect the output to a file if a name is given in the command line? For instance if I do</p>
  
  <pre><code>perl mymd5.perl md5file
  </code></pre>
  
  <p>then it will feed output to md5file. And if I just do</p>
  
  <pre><code>perl mydm5.perl 
  </code></pre>
  
  <p>it will just print to the console.</p>
  
  <p>This is my rootfile:</p>
  
  <pre><code>/usr/local/courses/cs3423/assign8/cmdscan.c
  /usr/local/courses/cs3423/assign8/driver.c
  /usr/local/courses/cs3423/assign1/xpostitplus-2.3-3.diff.gz
  </code></pre>
  
  <p>This is my program right now:      </p>
  
  <pre><code>open($in, "rootfile") or die "Can't open rootfile: $!";
  $flag = 0;
  
  if ($ARGV[0]){
          open($out,$ARGV[0]) or die "Can't open $ARGV[0]: $!";
          $flag = 1;
  }
  
  if ($flag == 1) {
          select $out;
  }
  
  while ($line = &lt;$in&gt;) {
          $md5line = `md5sum $line`;
          @md5arr = split(" ",$md5line);
          if ($flag == 0) {
                  printf("%s\t%s\n",$md5arr[1],$md5arr[0]);
          }
  }
  close($out);
  </code></pre>