storing a hash of a hash in a DBM database

C

Colvin

I'm trying to store a hash of a hash in a DBM database, but I keep
running into problems with my references. The enclosed script runs
without errors; the errors don't appear until I uncomment the commented
lines (the DBM functionality). How do I fix my references to make them
compatible with a DBM database?

Thank you,
Alec Colvin


----- error message ----------

13457 Can't use string ("HASH(0xba0c34)") as a HASH ref while "strict
refs" in use at c:\perl_test\database1.pl line 20.

----- code -------------------

use strict;

my %data;
# dbmopen(%data, "c:\\perl_test\\database", 0666) or
# die "Can't open the database";

my $person1 = { "fname" => "Stevie",
"lname" => "Wonder"
};

%data = ( "13457" => $person1,
);

my @ID_number = keys(%data);
@ID_number = sort(@ID_number);

my $i;
for ($i = 0; $i < @ID_number; $i++) {
print STDOUT "$ID_number[$i] ";
print STDOUT qq|$data{$ID_number[$i]}->{"fname"} |;
print STDOUT qq|$data{$ID_number[$i]}->{"lname"}\n|;
}

# dbmclose(%data);
 
R

Ragnar Hafstað

Colvin said:
I'm trying to store a hash of a hash in a DBM database, but I keep
running into problems with my references. The enclosed script runs
without errors; the errors don't appear until I uncomment the commented
lines (the DBM functionality). How do I fix my references to make them
compatible with a DBM database?

I assume you mean that you want the values of your tied hash to be
hashreferences.

in that case you might want to look at the module MLDBM (Multi-Level DBM)

gnari
 
P

pkent

Colvin <[email protected]> said:
I'm trying to store a hash of a hash in a DBM database, but I keep
running into problems with my references.
<snip>

The easiest thing to do is use MLDBM (in my opinion):

http://search.cpan.org/~chamas/MLDBM-2.01/lib/MLDBM.pm

It say:
"...store arbitrary perl data, including nested references. Thus, this
module can be used for storing references and other arbitrary data
within DBM databases."

But note the WARNINGS section, particularly whether there's a record
length limit in whichever DBM implementation you're using.

P
 
C

Colvin

Your advice was very useful. I've included a modified version of the
original script that now runs properly.

Thank you,
Alec Colvin

---------- code --------------

use strict;
use MLDBM 'DB_File';
use DB_File;

my %personnel;
tie(%personnel, 'MLDBM', 'testmldbm', O_CREAT|O_RDWR,
0640) or die "Couldn't tie the database";

my $person1 = { "fname" => "Stevie",
"lname" => "Wonder"
};

my $person2 = { "fname" => "Walter",
"lname" => "Cronkite"
};

%personnel = ( "13457" => $person1,
"23567" => $person2
);

my @ID_number = keys(%personnel);
my $i;
for ($i = 0; $i < @ID_number; $i++) {
print STDOUT "$ID_number[$i] ";
print STDOUT qq|$personnel{$ID_number[$i]}->{"fname"} |;
print STDOUT qq|$personnel{$ID_number[$i]}->{"lname"}\n|;
}

untie(%personnel);
 

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,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top