Tie::IxHash::Easy with "eval()"

P

Pat

Hi,

I'd like to use "Tie::IxHash" to maintain the order of keys in a hash.
Since the hash can contain other hashes (and so on), I need to use
"Tie::IxHash::Easy" so that the nested hashes also maintain their ordering.

I'm reading the entire hash from a file. The format of the file is exactly
like Data::Dumper so that I can build the hash by simply saying:

eval($file_contents);

This fails because there's no way to "tie" the internal workings of eval to
"Tie::IxHash::Easy" so it always just returns a normal hash. Copying the
returned into an IxHash has no effect because the ordering is already lost.

So, how can I load an IxHash from a file in Data::Dumper format and
preserve its order (including nested hashes)?

Thanks.
 
C

comp.llang.perl.moderated

Hi,

I'd like to use "Tie::IxHash" to maintain the order of keys in a hash.
Since the hash can contain other hashes (and so on), I need to use
"Tie::IxHash::Easy" so that the nested hashes also maintain their ordering.

I'm reading the entire hash from a file. The format of the file is exactly
like Data::Dumper so that I can build the hash by simply saying:

eval($file_contents);

This fails because there's no way to "tie" the internal workings of eval to
"Tie::IxHash::Easy" so it always just returns a normal hash. Copying the
returned into an IxHash has no effect because the ordering is already lost.

So, how can I load an IxHash from a file in Data::Dumper format and
preserve its order (including nested hashes)?

Using MLDBM with Tie::IxHash::Easy and the default serializer seems to
work for a single level, eg.

use MLDBM 'Tie::IxHash::Easy'
tie my %h, 'MLDBM' or die ...;
@h{qw/key1 key2 key3 /} = 1..3;
print Dumper \%h;

and for a multi-level hash, you could theoretically write and then use
a custom serialization package
to do this IIUC (see MLDBM doc.) ... eg.,

use MLDBM 'Tie::IxHash::Easy','MyIxSerializer';
use strict;
use warnings;
....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top