On Wed, Dec 17, 2003 at 02:00:23PM -0600, Andrew Sterling Hanenkamp wrote:
> I would like the ability to store a complicated record inside of a DBM
> file. I looked in the usual places and perldoc -q DBM gives me:
> 
>         Either stringify the structure yourself (no fun), or else get
>         the MLDBM (which uses Data::Dumper) module from CPAN and layer
>         it on top of either DB_File or GDBM_File.
>         
> Therefore, I went in search of a solution to automate the
> stringification. I didn't find anything other than MLDBM for doing
> something like this and it seems like a little much for my purposes. All
> I need is something like this:
> 
> $hash{name} = "value1:value2:value3:...";
> 
> I've done some work with Tie::Memoize and really like it's interface, so
> I decided to write something like it for wrapping hashes. Thus,
> Tie::HashWrapper was born. It may be used like this:
> 
> tie my %wrappee, 'AnyDBM_File', ...;
> tie my %wrapper, 'Tie::HashWrapper', \%wrappee,
>       -deflate_value => sub { join ':', @{$_[0]} },
>       -inflate_value => sub { split /:/, $_[0] };
> $wrapper{name} = [ value1, value2, value3 ];

I'd add a -1 to the split and not in the docs that the example
won't handle undefs.

> Does Tie::HashWrapper seem reasonable? Or does anyone have a better
> name? Have I gone off the deep-end again and rewritten something that
> already exists and I missed it?

I didn't like it at first but the more I try to think of alternatives,
and understand the purpose and use, the more I like it.

A key point is that although it was created for inflating/deflating
values, there's no need to use it for that. It does 'wrap' access to
the underlying hash and that wrapping can be used for other purposes,
including logging or recording where/how the hash is used.

It's similar in some ways to:
http://search.cpan.org/~pmqs/BerkeleyDB-0.25/BerkeleyDB.pod#DBM_Filters

And I think it would be worth making it more similar. Consider

  tie my %wrapper, 'Tie::HashWrapper', \%wrappee,
    store_key   => sub { lc(shift) },
    store_value => sub { join ':', @{$_[0]} },
    fetch_value => sub { lc(shift) },
    fetch_key   => sub { split /:/, $_[0], -1 };

Tim.

p.s. I trust your tests cover things like FIRSTKEY, NEXTKEY, DELETE etc.

Reply via email to