Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Tying Complex Data Structures using dbmfilters

by jgallagher (Pilgrim)
on Jan 13, 2005 at 04:01 UTC ( [id://421866]=perlquestion: print w/replies, xml ) Need Help??

jgallagher has asked for the wisdom of the Perl Monks concerning the following question:

I've searched for a simple answer to whether or not there is an easy way to use a tied hash (e.g., to NDBM_File) to store a complex data structure. Unfortunately, MLDBM is not an option and potentially overkill. I have a couple of questions about the following in which I basically use dbmfilters to serialize and unserialize the data:
#!/usr/bin/perl -w use strict; use NDBM_File; use Fcntl; use Data::Dumper; $Data::Dumper::Indent = 0; $Data::Dumper::Purity = 1; $Data::Dumper::Terse = 1; my %hash; my $filename = "test_ndbm"; my $db = tie(%hash, 'NDBM_File', $filename, O_RDWR|O_CREAT, 0640) or die "Cannot open $filename: $!\n"; $db->filter_store_value( sub { $_ = Dumper($_); } ); $db->filter_fetch_value( sub { $_ = eval($_) } ); my %to_be_stored = ( this_is => { some_test => 'data' }, which_is => 'fairly', complex => [0, 1, 2], ); my @keys = keys %hash; my $new_id = scalar(@keys); $hash{$new_id} = \%to_be_stored; foreach my $key (keys %hash) { foreach (keys %{$hash{$key}}) { print "hash{$key}{$_} = " . Dumper($hash{$key}{$_}) . "\n"; } } undef $db; untie $hash;
First, is there something I'm missing about the above that will come back to haunt me? Secondly, this method apparently requires the use of an intermediate hash (%to_be_stored in the above). For example, something like the following:
$hash{a_key} = { b_key => 2 }; $hash{a_key}{b_key} = 3;
results in $hash{a_key}{b_key} being equal to 2. I believe this is because the filters (and the tying in general) only fire off when actually updating the value directly, i.e., $hash{a_key}. Is there any way to work around this?

Replies are listed 'Best First'.
Re: Tying Complex Data Structures using dbmfilters
by broquaint (Abbot) on Jan 13, 2005 at 04:11 UTC
    Perhaps DBM::Deep is the module you're looking for since it describes itself as A pure perl multi-level hash/array DBM, and rather conveniently has a tie interface.
    HTH

    _________
    broquaint

      That actually solves my problem perfectly; unfortunately installing CPAN modules is not an option. Fortunately this does not have to be extremely robust or speedy, just correct, and, if possible, easy to use (for my sake). :-)

        DBM::Deep is pure-Perl, so installation issues do not apply. You just need to copy the source somewhere accessible.

        Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://421866]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-19 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found