jgallagher has asked for the wisdom of the Perl Monks concerning the following question:
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:#!/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;
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?$hash{a_key} = { b_key => 2 }; $hash{a_key}{b_key} = 3;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Tying Complex Data Structures using dbmfilters
by broquaint (Abbot) on Jan 13, 2005 at 04:11 UTC | |
by jgallagher (Pilgrim) on Jan 13, 2005 at 04:21 UTC | |
by Aristotle (Chancellor) on Jan 13, 2005 at 04:32 UTC | |
by jgallagher (Pilgrim) on Jan 13, 2005 at 05:22 UTC | |
by merlyn (Sage) on Jan 13, 2005 at 05:38 UTC | |
| |
by Aristotle (Chancellor) on Jan 13, 2005 at 06:28 UTC | |
by dragonchild (Archbishop) on Jan 13, 2005 at 14:14 UTC |