title: perl - best way to create gzipped files
body: <p>I need to alter my routine and have the final outfile be gzipped. I'm trying to figure out what is the best way to gzip a processed file called within a perl subroutine.</p>
  
  <p>For example, I have a sub routine that creates the file (extract_data). 
  Here's the main loop and sub routine:</p>
  
  <pre><code>foreach my $tblist (@tblist)
  {
     chomp $tblist;
     extract_data($dbh, $tblist);
  };
  $dbh-&gt;disconnect;
  
  sub extract_data
  {
       my($dbh, $tblist) = @_;
       my $final_file = "/home/proc/$node-$tblist.dat";
       open (my $out_fh, '&gt;', $final_file) or die "cannot create $final_file: $!";
       my $sth = $dbh-&gt;prepare("...");
       $sth-&gt;execute();
       while (my($uid, $hostnm,$col1,$col2,$col3,$upd,$col5) = $sth-&gt;fetchrow_array() ) {
         print $out_fh "__my_key__^A$uid^Ehost^A$hostnm^Ecol1^A$col1^Ecol2^A$col2^Ecol3^A$col3^Ecol4^A$upd^Ecol5^A$col5^D";
       }
       $sth-&gt;finish;
       close $out_fh or die "Failed to close file: $!";
  };
  </code></pre>
  
  <p>Do I do the gzip within the main or with the sub? What is the best way to do so?
  Then my new file would be <code>$final_file =/home/proc/$node-$tblist.dat.gz</code></p>
  
  <p>thanks. </p>