Perl is the best way to create gzipped files

I need to change my routine and get the final outgz file. I am trying to figure out what is the best way for a gzip processed file to be called inside the perl routine.

For example, I have a routine that creates a file (extract_data). Here's the main loop and subroutine:

foreach my $tblist (@tblist)
{
   chomp $tblist;
   extract_data($dbh, $tblist);
};
$dbh->disconnect;

sub extract_data
{
     my($dbh, $tblist) = @_;
     my $final_file = "/home/proc/$node-$tblist.dat";
     open (my $out_fh, '>', $final_file) or die "cannot create $final_file: $!";
     my $sth = $dbh->prepare("...");
     $sth->execute();
     while (my($uid, $hostnm,$col1,$col2,$col3,$upd,$col5) = $sth->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->finish;
     close $out_fh or die "Failed to close file: $!";
};

Am I doing gzip inside main or with sub? What is the best way to do this? Then my new file will be$final_file =/home/proc/$node-$tblist.dat.gz

thank.

+3
source share
2 answers

I know that there are modules for this, without using external programs, but since I understand how to use it gzipmuch better than I understand how to use these modules, I just open the process gzipand call it daily.

open (my $gzip_fh, "| /bin/gzip -c > $final_file.gz") or die "error starting gzip $!";
...
while (... = $sth->fetchrow_array()) {
    print $gzip_fh "__my_key__^A$uid^Ehost^A$hostname..."; # uncompressed data
}
...
close $gzip_fh;
+10
source

IO:: Compress:: Gzip, Perl:

use IO::Compress::Gzip qw(gzip $GzipError) ;

my $z = new IO::Compress::Gzip($fileName);
  or die "gzip failed: $GzipError\n";

# object interface
$z->print($string);
$z->printf($format, $string);
$z->write($string);
$z->close();

# IO::File mode
print($z $string);
printf($z $format, $string);
close($z);

perldoc

FWIW, IO:: Uncompress:: Gunzip gzip .

+2

Source: https://habr.com/ru/post/1795862/


All Articles