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?
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With