Skip to content

Instantly share code, notes, and snippets.

@andrewyatz
Created February 16, 2011 15:09
Show Gist options
  • Save andrewyatz/829523 to your computer and use it in GitHub Desktop.
Save andrewyatz/829523 to your computer and use it in GitHub Desktop.
A way of plugging a storable call into a compress call & then ripping this back out @ the other end
use strict;
use warnings;
use Data::Dumper; # Not required
use Storable qw(nfreeze thaw);
use IO::Compress::Gzip qw(gzip $GzipError); # qw() imports only for functional
use IO::Uncompress::Gunzip qw(gunzip $GunzipError); # qw() imports only for functional
#Just doing some quick obj hacks
my $v = bless({ b => bless({ a => 1, c => [qw(x y z)]}, 'My::A') }, 'My::B');
#And showing what it looks like
warn '---First---';
warn Dumper($v);
my $frozen = nfreeze($v);
warn '---Frozen---';
warn Dumper $frozen;
# The OO way
# my $gzipped_and_frozen;
# my $gz = IO::Compress::Gzip->new( \$gzipped_and_frozen, -LEVEL => 9);
# $gz->write($frozen);
# $gz->close();
# But the functional way is kinda nicer
my $gzipped_and_frozen;
my $gzip = gzip \$frozen => \$gzipped_and_frozen, -LEVEL => 9 or die "gzip failed: $GzipError";
warn '---Gzipped---';
warn Dumper $gzipped_and_frozen;
# #Now to bring it back using OO
# my $gunzip = IO::Uncompress::Gunzip->new(\$gzipped_and_frozen);
# my $gunzipped_and_frozen;
# {
# local $/ = undef;
# $gunzipped_and_frozen = $gunzip->getline();
# }
# $gunzip->close();
# These are the ways to do it functionally
my $gunzipped_and_frozen;
gunzip \$gzipped_and_frozen => \$gunzipped_and_frozen or die "gunzip failed: $GunzipError";
warn '---Gunzipped---';
warn Dumper $gunzipped_and_frozen;
my $back_from_the_freezer = thaw($gunzipped_and_frozen);
warn '---Back---';
warn Dumper $back_from_the_freezer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment