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

I'm using the Storable module to save and load a hash. I also want to use the Hash::Case::Lower module to make my hash case insensitive. I can't figure out how to tie a hashref to the Hash::Case::Lower module and load data into it with Storable. I tried something like:
use Hash::Case::Lower; use Storable; tie my(%realhash), 'Hash::Case::Lower'; my $hashref = \%realhash; $hashref = retrieve('somefile');
This causes $hashref to be assigned to the anonymous hash returned from retrieve() so that it no longer points to the tied hash. It is then no longer case insensitive. How can I make this work?

Replies are listed 'Best First'.
Re: using Hash::Case and Storable together
by tkil (Monk) on Apr 26, 2004 at 07:53 UTC

    Without testing either of these, I suspect that you have two options: (1) iterate through the retrieved hash, inserting key/value pairs into a newly-created Hash::Case::Lower object; or (2) go behind the back of Hash::Case::Lower to both dump out the hash, and load it back in.

    The former is straightforward, and probably looks something like this:

    tie my(%lc_hash), 'Hash::Case::Lower'; { my $href = retrieve( ... ); while ( my ( $k, $v ) = each %$href ) { $lc_hash{$k} = $v; } }

    Although, if you're going through the whole thing anyway, there's no problem in applying lc to all the values manually. Might be faster...

    To go behind its back, you will want to save it directly; the only fancy footwork should be a bless when you read it back in. (This might not even be necessary — I don't know how clever Storable is, really.)

    # writing it out store( \%lc_hash, ... ); # reading it in my $lc_href = retrieve( ... ); bless $lc_href, 'Hash::Case::Lower';

    All the above is untested, but hopefully it will give you some ideas to play with! :)

Re: using Hash::Case and Storable together
by eserte (Deacon) on Apr 26, 2004 at 12:26 UTC
    Try to store and retrieve the object behind the tied hash. I.e.: store(tied(%realhash), "file");

    After retrieving the object, use the Tie::Restore module to get the tied hash again.

      I ended up taking the easy way out and copying the entire structure with a 'foreach(keys %retrievedhash)' or something similar to the tied hash. Thanks for the suggestions, though.