Storing Object in DBM Hash

M

michael.shnitzer

Why would the following code work in a regular hash environment but
dereference when using a DBM hash?

"Can't locate object method "docno" via package
"Document=HASH(0x4001d0cc)" (perhaps you forgot to load
"Document=HASH(0x4001d0cc)"?) at read.pl line 6."

#! /usr/bin/perl -w
use strict;

dbmopen(my %hash, "db", 0777);
$hash{mike} = new Document;
print $hash{mike}->docno;

package Document;
sub new
{
my $self = {
document_number => "DOC123",
};
bless $self;
}
sub docno
{
my $self = shift;
return $self->{document_number};
}
 
M

michael.shnitzer

On May 16, 2:25 pm, (e-mail address removed) wrote:

<snip>

Sorry for the double post.
 
A

anno4000

Why would the following code work in a regular hash environment but
dereference when using a DBM hash?

The values of DBM-Tied hashes can only be plain scalars, not references.
After all, references are live addresses in the current program, unfit
to be stored to disk.

CPAN has MLDBM (where ML stands for Multi Level) which deals with
that problem. There may be newer modules for the same purpose.

Anno
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top