SPUG: Tied Hashes: Works with Nested Hashes?

Doug Beaver dougb at scalar.org
Thu Feb 3 21:30:51 CST 2000

On Thu, Feb 03, 2000 at 06:51:24PM -0800, Tim Maher/CONSULTIX wrote:
> For the first time, I'm trying to "tie" a non-trivial hash  (one with
> values that are references to other "nested" hashes), and having
> trouble.
> With the "dbmopen() or die" and "dbmclose()" calls commented-out, I
> can load, manipulate, and print-out my hash structure just fine; when
> I remove the comment symbols, all I seem to be left with in my complex
> data structure is the keys for the uppermost hash!  The *.dir, *.pag
> files DO get created, and loaded with stuff.
> I don't see any statement anywhere that you can only tie simple
> hashes, but now I'm wondering if that's the limitation.

It is a limitation, using the normal tie modules you end up storing
the string value of the reference if you try to stick something complex
in the hash.  Check out MLDBM on CPAN, it uses Data::Dumper (or
FreezeThaw or Storable) to pack the data structure appropriately.

If you use MLDBM, there's also a gotcha because of limitations with
perl's tie mechanism, you have to do this:

$ref = $tied_hash{$key};
$ref->{'blah'}->{'blah'} = 'blah';
$tied_hash{$key} = $ref;

instead of:

$tied_hash{$key}->{'blah'}->{'blah'} = 'blah';

The MLDBM docs cover everything you need though...

Also, dbm* were carried over from perl4, you should use tie() so that
you can use DB_File.  It's btree features allow you to store the keys in
sorted order for really fast key access, and you can create your own
comparison functions that are used when you sort the keys, so you can
create dictionaries that store their keys in a case insensitive way
(which can be quite useful).  Also, there are no size limits on the
values, and the speed stays really fast even with dbs that are over a
gig in size.

I'm biased though, I really like Berkeley DBs.  Good luck with your



Smithers: I'm afraid we have a bad image, Sir.  Market research shows
          people see you as somewhat of an ogre.
   Burns: I ought to club them and eat their bones!

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address

More information about the spug-list mailing list