Storage of an object on the database and its extraction

I have a perl object (a reference to an array of links), as shown below:

my $a = [ [$a, $ab, $c ], [$a, $b, $c] ] ;

and you need to save it in the database, and then extract it.

Can anyone suggest a good mechanism for serializing even for compression, and then save it to the database? Then deserialize it and use in code?

+5
source share
3 answers

You can use any of the famous serializers, for example. JSON :: XS or Storable . Storableit's better if you want to get links as links, and not as copies of values. Then save the serialized object in a field of any type (VARCHAR, BLOB, ...) that satisfy the 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;

, $bytestream, , IO::Compress::Gzip

my $bytestream = gzip nfreeze $data;
+7

, perldoc , Data:: Dumper ", ". Dumper .

-1

How about data: Dumper? You can dump objects in the TEXT field in the database and then analyze the contents to return it.

-1
source

All Articles