Simple multi-level tie
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 ]; and so forth. In addition, if one wants to have more complicated keys, one may add -deflate_key/-inflate_key values to the call to tie. I haven't uploaded it CPAN yet pending documentation and finding a good name. 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? Cheers, Sterling -- Andrew Sterling Hanenkamp http://Andrew.Sterling.Hanenkamp.com/ [EMAIL PROTECTED] / [EMAIL PROTECTED] There exists in the human heart a depraved taste for equality, which impels the weak to attempt to lower the powerful to their own level, and reduces men to prefer equality in slavery to inequality under freedom. -- Alexis de Tocqueville
Re: Simple multi-level tie
On Wed, Dec 17, 2003 at 02:00:23PM -0600, Andrew Sterling Hanenkamp wrote: 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: When I want to do this, I just use CGI.pm. With it, you can pass a hash to the 'new' constructor, and use query_string() function (I think) to get back a stringified version. Likewise, you can pass a query-string to the constructor, and get a hash structure back. Keys with multiple values are supported, although I usually don't have that case. Your solution may well be cleaner for general cases. Mark
Re: Simple multi-level tie
On Wed, 2003-12-17 at 14:00, Andrew Sterling Hanenkamp wrote: 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 ... As I understand it, the standard way to (de)marshall things anymore is to use Storable. see DirDB::Storable for an example of a multi-level tie that punts anything other than scalars and unblessed hashrefs to Storable for nstorage and retreival. If you're not going to have more than a thousand records (or you have reiserfs) DirDB might be the module for you rather than a single-file database. -- david nicol Where the hell did I put my coffee?
Re: Simple multi-level tie
On Wed, 2003-12-17 at 17:15, david nicol wrote: As I understand it, the standard way to (de)marshall things anymore is to use Storable. see DirDB::Storable for an example of a multi-level tie that punts anything other than scalars and unblessed hashrefs to Storable for nstorage and retreival. This is yet another corollary to my solution. If you like Storable then: use Storable qw(freeze thaw); use Tie::HashWrapper; tie my %wrappee, 'AnyDBM_File', ...; tie my %hash, 'Tie::HashWrapper', \%wrappee, -inflate_value = sub { thaw(shift) }, -deflate_value = sub { freeze(shift) }; $hash{a}{complicated}[4]{data} = [ 'structure' ]; If you're not going to have more than a thousand records (or you have reiserfs) DirDB might be the module for you rather than a single-file database. Ah, Hubris, Randall Schwartz and the Great One would be proud. ;) Alas, either I am being misunderstood or no one has an answer to my questions. I believe I was asking for it with the subject line of Simple multi-level tie. What I should have wrote is Wrapping hashes with arbitrary inflate/deflate methods. This is a tool that adds syntactic sugar to hashes. I developed it for the purpose of making complicated storage in hashes tied to DBM files nicer. It doesn't matter if you use CGI::query_string, Storable, join/split, pack/unpack, or even just use this to do some kind of wacky filtering, this tool isn't a marshalling tool, it is a syntax helper. What I want to know is, is Tie::HashWrapper a good name? If you don't like that name, what might you call it? HashWrapper sounds kind of dorky to me, but that's what first came to mind and I didn't want to spend all day trying to name it, I wanted to play code monkey. Cheers, Sterling -- Andrew Sterling Hanenkamp http://Andrew.Sterling.Hanenkamp.com/ [EMAIL PROTECTED] / [EMAIL PROTECTED] Microsoft is a cross between the Borg and the Ferengi. Unfortunately, they use Borg to do their marketing and Ferengi to do their programming. -- Simon Slavin
Re: Simple multi-level tie
On 12/18/2003 12:25 AM, Andrew Sterling Hanenkamp wrote: Wrapping hashes with arbitrary inflate/deflate methods. This is a tool that adds syntactic sugar to hashes. I developed it for the purpose of making complicated storage in hashes tied to DBM files nicer. It doesn't matter if you use CGI::query_string, Storable, join/split, pack/unpack, or even just use this to do some kind of wacky filtering, this tool isn't a marshalling tool, it is a syntax helper. What I want to know is, is Tie::HashWrapper a good name? If you don't like that name, what might you call it? HashWrapper sounds kind of dorky to me, but that's what first came to mind and I didn't want to spend all day trying to name it, I wanted to play code monkey. It's a horrible name. Sorry. :-) The name needs to say what it is or what it's for, not how it's done. Some people hate the Tie namespace (I'm one of them), but it has become standard for tied modules. So: Tie::MLDBM::Any_* (something) or Tie::MLDBM::Custom_* (something) where * says something about having custom marshalling methods. (Store|Marshall|Format|Dump|Load|Stow|...) Regards, Randy.
RE: Simple multi-level tie
I actually like names that sound like foodg! On a slightly more serious note---If I were looking for stringification, I wouldn't be looking under HashWrapper. Perhaps Tie::HashStringify or the like? --hsm -Original Message- From: Andrew Sterling Hanenkamp [mailto:[EMAIL PROTECTED] Sent: Wednesday, December 17, 2003 1:00 PM To: Module Authors Subject: Simple multi-level tie 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 ]; and so forth. In addition, if one wants to have more complicated keys, one may add -deflate_key/-inflate_key values to the call to tie. I haven't uploaded it CPAN yet pending documentation and finding a good name. 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? Cheers, Sterling -- Andrew Sterling Hanenkamp http://Andrew.Sterling.Hanenkamp.com/ [EMAIL PROTECTED] / [EMAIL PROTECTED] There exists in the human heart a depraved taste for equality, which impels the weak to attempt to lower the powerful to their own level, and reduces men to prefer equality in slavery to inequality under freedom. -- Alexis de Tocqueville