Skip to content

Instantly share code, notes, and snippets.

@cghiban
Created February 13, 2020 17:03
Show Gist options
  • Save cghiban/50bb898984f003ad0a54241c67a5ab88 to your computer and use it in GitHub Desktop.
Save cghiban/50bb898984f003ad0a54241c67a5ab88 to your computer and use it in GitHub Desktop.
gzip-freeze-thaw-ungzip
use IO::Compress::Gzip qw(gzip $GzipError);
use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
use Storable qw(nfreeze thaw);
use IO::Scalar ();
my $input = "ABC" x 200 . "A" x 2000;
my $output = "";
my $gh = IO::Scalar->new(\$output);
print length($input), $/;
gzip \$input => $gh
or die "gzip failed: $GzipError\n";
print length($output), $/;
my $data_to_send = nfreeze(\$output);
print length($data_to_send), $/;
my $data_received = thaw $data_to_send;
print length($$data_received), $/;
my $ungzipped_data = '';
gunzip $data_received => \$ungzipped_data;
print length($ungzipped_data), $/;
print "same data: ", $input eq $ungzipped_data, "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment