Reputation: 3262
I have a perl object (reference to array of references) like the below:
my $a = [ [$a, $ab, $c ], [$a, $b, $c] ] ;
and need to store it on the DB then retrieve it.
Could someone suggest a good mechanism to serialize even to compress it and then store it on the DB? Then deserialize it and use it in the code?
Upvotes: 5
Views: 1565
Reputation: 206
You may use any of the known serializers, e.g. JSON::XS or Storable.
Storable
is better if you want to retrieve references as references, not as copies of values. Then save a serialized object in the field of any type (VARCHAR, BLOB, ...) that satisfy storage requirements.
use Storable qw(nfreeze thaw);
use DBI;
# ... connect to database
# Store
my $data = [ [$a, $b, $c ], [ $a, $b, $c ] ];
my $bytestream = nfreeze $data;
$dbh->do('insert into table (field) values(?)', undef, $bytestream);
# Retrieve
$bytestream = $dbh->selectrow_array('select field from table where ...');
$data = thaw $bytestream;
Additionally, you can compress $bytestream
, for example, via IO::Compress::Gzip
my $bytestream = gzip nfreeze $data;
Upvotes: 7
Reputation: 134
What about Data::Dumper? You could dump the objects into a TEXT field of the DB and then eval the content to get it back.
Upvotes: -1
Reputation: 5090
I've never tried it myself, but the perldoc says the return value of Data::Dumper can be "evaled to get back an identical copy of the original reference structure". You could then put the Dumper output into a large enough text field in the database.
Upvotes: -1