Re: Store Object Class in DBM Hash
- From: "Mumia W." <paduille.4061.mumia.w+nospam@xxxxxxxxxxxxx>
- Date: Wed, 16 May 2007 20:29:49 GMT
On 05/16/2007 11:41 AM, michael.shnitzer@xxxxxxxxx wrote:
It was my understanding that once a DBM hash was tied to a file, it
can be accessed using the same methods as a regular hash. For
simplicities sake I created a small perl program to demonstrate what I
am trying to do:
***********************************
#! /usr/bin/perl -w
use warnings;
my %hash = ( );
dbmopen(%hash, ".data/documents", 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};
}
**************************************
If I run this program with the DBM hash it returns "Can't locate
object method "docno" via package "Document=HASH(0x4001d0cc)" (perhaps
you forgot to load "Document=HASH(0x4001d0cc)"?) at read.pl line 11."
But if I run this program without opening the DBM hash, then the
program returns the expected result of DOC123.
Obviously there is a difference in accessing the hash data when it is
being read from a DBM hash that I am missing.
Any suggestions would be appreciated.
Thanks,
--Mike
A couple of limitations in Perl and dbmopen expose the fact that DBM-hashes are not real hashes; to store objects in a DBM, you'd probably have to Serialize the data. You can either serialize it yourself, or you can let MLDBM serialize it for you:
#!/usr/bin/perl
use strict;
use warnings;
my $use_mldbm = 1;
if (! $use_mldbm)
{
use Storable qw(freeze thaw);
my $file = 'dfiles/documents';
my %hash;
dbmopen %hash, $file, 0644 or die("Oops: $!");
my $obj = Document->new;
$obj->increment_docno;
$obj->increment_docno;
$obj->increment_docno;
$obj->increment_docno;
$hash{mike} = freeze($obj);
print thaw($hash{mike})->docno, "\n"; # prints "DOC127"
dbmclose %hash;
exit;
}
if ($use_mldbm)
{
use MLDBM qw(DB_File Storable);
my $file = 'dfiles/docs.db';
my %hash;
tie %hash, 'MLDBM', $file;
my $obj = Document->new;
$obj->increment_docno;
$obj->increment_docno;
$obj->increment_docno;
$obj->increment_docno;
$hash{mike} = $obj;
print $hash{mike}->docno, "\n";
untie %hash;
exit;
}
package Document;
sub new
{
my $self = {
document_number => "DOC123",
};
bless $self;
}
sub docno
{
my $self = shift;
return $self->{document_number};
}
sub increment_docno {
my $self = shift;
$self->{document_number} =~ s/(\d+)$/$1 + 1/e;
$self->{document_number};
}
__END__
Be sure to read the admonition in the BUGS section of MLDBM's pod; it indirectly applies to dbmopen also.
.
- References:
- Store Object Class in DBM Hash
- From: michael . shnitzer
- Store Object Class in DBM Hash
- Prev by Date: Test harness for scripts?
- Next by Date: Re: Simple Regular Expression Help
- Previous by thread: Store Object Class in DBM Hash
- Next by thread: Re: Store Object Class in DBM Hash
- Index(es):
Relevant Pages
|
|