For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2004 > using Hash::Case and Storable









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author using Hash::Case and Storable
Andrew Gaffney

2004-04-27, 12:23 am

I'm using the Storable module to save and load a hash tree. 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?

--
Andrew Gaffney
Network Administrator
Skyline Aeronautics, LLC.
636-357-1548

Wiggins D Anconia

2004-04-27, 12:23 am

> I'm using the Storable module to save and load a hash tree. 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?
>


You need to tie your hash after retrieving the values. Or provide the
values as part of the tie invocation. I haven't worked with Hash::Case
before, but it looks from the docs that you can provide your retrieved
hashref as the 'VALUES'. For instance,

my $thawed = retrieve('somefile');
tie my(%realhash), 'Hash::Case::Lower', $thawed;

That or you would have to walk the structure of what you have installed
and add each piece to the hash again, or use a hash slice to do it.

@realhash{ keys %$thawed } = @$thawed{ keys %$thawed }; # untested!

This should guarantee that the TIE methods are invoked properly.

But then I am just guessing :-)...

http://danconia.org
Andrew Gaffney

2004-04-27, 12:26 am

Wiggins d Anconia wrote:
>
> want to use the
>
>
> figure out how to tie a
>
>
> Storable. I tried
>
>
> from retrieve() so that
>
>
> insensitive. How can I
>
>
> You need to tie your hash after retrieving the values. Or provide the
> values as part of the tie invocation. I haven't worked with Hash::Case
> before, but it looks from the docs that you can provide your retrieved
> hashref as the 'VALUES'. For instance,
>
> my $thawed = retrieve('somefile');
> tie my(%realhash), 'Hash::Case::Lower', $thawed;


Ah, I guess I'd missed that part in the docs for the Hash::Case::Lower module. Is it safe
to 'delete $thawed' after I tie the hash so I don't have an extra copy of the hash
floating around?

--
Andrew Gaffney
Network Administrator
Skyline Aeronautics, LLC.
636-357-1548

Wiggins D Anconia

2004-04-27, 12:26 am

> Wiggins d Anconia wrote:

[snip]

>
> Ah, I guess I'd missed that part in the docs for the Hash::Case::Lower

module. Is it safe
> to 'delete $thawed' after I tie the hash so I don't have an extra copy

of the hash
> floating around?
>


Try and see. A look at the source suggests there isn't a problem doing
it, if there is I would think it is an implementation bug. I assume by
"delete" you mean allow the variable to go out of scope or otherwise get
garbage collected, as opposed to 'delete' as in -f.

You may also want to consider that nested hashes (if my understanding
and glance at the code are correct) are not necessarily lower case.
This might be either a suggested improvement, or even a new subclass of
Tie::Hash, aka Tie::Hash::LowerDeep or similar, essentially you would
end up with a nested call to tieing any internal hash refs.

http://danconia.org
Andrew Gaffney

2004-04-27, 12:26 am

Wiggins d Anconia wrote:
>
>
> [snip]
>
>
>
> module. Is it safe
>
>
> of the hash
>
>
> Try and see. A look at the source suggests there isn't a problem doing
> it, if there is I would think it is an implementation bug. I assume by
> "delete" you mean allow the variable to go out of scope or otherwise get
> garbage collected, as opposed to 'delete' as in -f.


Correct.

> You may also want to consider that nested hashes (if my understanding
> and glance at the code are correct) are not necessarily lower case.
> This might be either a suggested improvement, or even a new subclass of
> Tie::Hash, aka Tie::Hash::LowerDeep or similar, essentially you would
> end up with a nested call to tieing any internal hash refs.


I'd realized that. I don't need the nested hashes to be lower case, so it is not an issue.

--
Andrew Gaffney
Network Administrator
Skyline Aeronautics, LLC.
636-357-1548

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com