Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl Complex data structure in Berkeley DB

Tags:

perl

folks

could someone please shed some light here

this script:

use strict;
use BerkeleyDB;
my $filename = "/tmp/test" ; 
unlink $filename ;
tie my %h, "BerkeleyDB::Hash", -Filename => $filename,-Flags    => DB_CREATE or die   
"Cannot open file $filename: $! $BerkeleyDB::Error\n" ;
$h{id1}{11111}{red}{2222}{3333}=1;
$h{id1}{11111}{red}{2222}{223}=1;
print "$h{id1}{11111}{red}{2222}{3333}";
untie %h ;

drop an alert: "Can't use string ("HASH(0x822e638)") as a HASH ref while "strict refs" in use at ./ber2.pl line 17.

Line 17 is this : $h{id1}{11111}{red}{2222}{223}=1;

what is the problem with this?

if I run script without Berkeley it works like a charm, but i need to use the DB, as i have memory issues

please help

like image 888
hu3b11b7 Avatar asked May 10 '26 05:05

hu3b11b7


1 Answers

You can't do this, the BerkeleyDB tied hash interface can only store plain scalars, not references (so no complex data structures). You can store serialized data (e.g. from Storable) in it, or have multiple hashes, and use some of them to store keys into other ones (you'd have to do all of the work for accessing this on your own, though). Or you could use something completely different like DBM::Deep, KiokuDB (has a BerkeleyDB backend), or DBIx::Class + SQLite.

like image 162
hobbs Avatar answered May 12 '26 15:05

hobbs