How to serialize a perl object in a .gz file

P

Peng Yu

Uh, gzip the data once you've serialized it? You can use
IO::Compress::Gzip to do the compression for you, and you can use
Storable::freeze rather than Storable::store to avoid the temporary file
if your data's small enough.

Will there be roughly the same level of compression rate if I store
the freezed object compared with that I gzip the compress the file
stored without freeze?

I got the following error. Would you please let me know what I am
wrong?
cat freeze.pl
#!/usr/bin/perl

use strict;
use warnings;

use Storable;
use Data::Dumper;

my %hash = (
a=>[1,2,3],
b=>['x','y','z'],
);

my $serialized = freeze \%hash;
store \$serialized, "freeze.pdata";
print Dumper(\$serialized);
./freeze.pl
Backslash found where operator expected at ./freeze.pl line 14, near
"freeze \"
(Do you need to predeclare freeze?)
syntax error at ./freeze.pl line 14, near "freeze \"
Execution of ./freeze.pl aborted due to compilation errors.
 
K

kath

Uh, gzip the data once you've serialized it? You can use
IO::Compress::Gzip to do the compression for you, and you can use
Storable::freeze rather than Storable::store to avoid the temporary file
if your data's small enough.

Will there be roughly the same level of compression rate if I store
the freezed object compared with that I gzip the compress the file
stored without freeze?

I got the following error. Would you please let me know what I am
wrong?
cat freeze.pl

#!/usr/bin/perl

use strict;
use warnings;

use Storable;
use Data::Dumper;

my %hash = (
a=>[1,2,3],
b=>['x','y','z'],
);

my $serialized = freeze \%hash;
store \$serialized, "freeze.pdata";
print Dumper(\$serialized);
./freeze.pl

Backslash found where operator expected at ./freeze.pl line 14, near
"freeze \"
(Do you need to predeclare freeze?)
syntax error at ./freeze.pl line 14, near "freeze \"
Execution of ./freeze.pl aborted due to compilation errors.

I haven't used 'store' anytime. So i cannot comment on that part. You
last line should be,
print Dumper $serialized;

katharnakh.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top