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

Hello again Monks,

I want to start using hashes a bit for some coding I am doing to keep a list of friends with information I can use to better keep in touch with them. I want to create this with thoughts of expanding this later. I have used DB_File to tie a hash to a file. Thanks for the help with that fellow monks. Now, I want to know if I can tie a hash of hashes to a file. I tried to use DB_File but with no luck. When the file is read, it identifies the entries are there, but fails to return any of the information for each entry. Here is the code I am playing with.

#!/usr/bin/perl -w # The information contained within this file is fictional # No intent to link this information to real people has been made #use strict; use warnings; # The following module was created to help tie hashes to files use DB_File; my $showOutput = 1; my $start_time = time(); # Name of the file that will be tied to the hash my $hash_filename = "hash_of_hashes.dbf"; my $remove_hash = shift; if ( $remove_hash eq "NEW" ){ print STDOUT "Deleting old hash file\n" if $showOutput; unlink $hash_filename or die "Can not remove $hash_filename\n\n"; } else{ print STDOUT "Using old hash file\n" if $showOutput; } # Tie the file to the hash tie my %hash, 'DB_File', $hash_filename, O_RDWR|O_CREAT, 0666, $DB_HAS +H or die "Problem tying \%hash: $!\n\n"; if ( exists $hash{"Janet Jones"} ){ print STDOUT "JJ has already been defined\n"; # Do nothing, person is already defined } else{ print STDOUT "JJ does NOT exist... defining\n"; $hash{"Janet Jones"} = { "spouse" => "Bob Jones", "son 1" => "Tom Jones", "son 2" => "Pete Jones", "pet 1" => "Boyle", "father" => "Richard Smith", "mother" => "Fran Smith", "sister 1" => "Louise Williams", "address" => "1 Main St, Littleton, PA, 55555", }; } if ( exists $hash{"Bob Jones"} ){ print STDOUT "BJ has already been defined\n"; # Do nothing, person is already defined } else{ print STDOUT "BJ does NOT exist... defining\n"; $hash{"Bob Jones"} = { "spouse" => "Janet Jones", "son 1" => "Tom Jones", "son 2" => "Pete Jones", "pet 1" => "Boyle", "father" => "Paul Jones", "mother" => "Stella Jones", "brother 1" => "Paul Jones, Jr", "brother 2" => "Vince Jones", "brother 3" => "Dan Jones", "sister 1" => "Andrea Limux", "address" => "1 Main St, Littleton, PA, 55555", }; } for my $name ( keys %hash ){ print STDOUT "$name:\n"; for my $info ( sort keys %{ $hash{$name} } ) { print "\t$info: " . $hash{$name}{$info} . "\n"; } } untie %hash; my $end_time = time(); print STDOUT "\n\n"; print STDOUT "Start Time: $start_time\n"; print STDOUT "End Time: $end_time\n"; print STDOUT "Total Time: " . ($end_time - $start_time) . " seconds\n\ +n";


I don't have to use DB_File. Since I used it for tying a hash to a file, I was thinking it would support tying hash of hashes to a file, too. Any insights in this area would be very much appreciated. If I don't tie the file to the hash, and just keep the hash in memory, this all works fine, but I want to tie it to a file to keep this as a record for updates, etc.

As always, thank you very much for your help.

Replies are listed 'Best First'.
Re: Can I tie a hash of hashes to a file?
by moritz (Cardinal) on Nov 11, 2009 at 13:49 UTC
    I think DBM::Deep might be what you're looking for.
    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Can I tie a hash of hashes to a file?
by davido (Cardinal) on Nov 11, 2009 at 13:55 UTC

    One problem that you'll run into is that your tie, behind the scenes, is writing the contents of the top level hash elements to a file. That's similar to printing the top level element. Your top level elements contain hash-refs. When you print a reference, it stringifies the reference and prints that to the file rather than dereferencing the reference and storing it hierarchically in the file. So your suspicions are correct that a tie is not multi-dimensional, unless you somehow concoct a tied class that handles the complexities of dismantling and reconstructing the data structure for you.


    Dave

Re: Can I tie a hash of hashes to a file?
by biohisham (Priest) on Nov 11, 2009 at 15:58 UTC
    I think the module Storable can be of help, It has the methods 'store' and 'retrieve' which are direct to work with when storing datastructures to files and returning them back,in addition to other utilities, check the link above...In my Windoze Machine, the DB_File module did not really work for me, so I used this one instead...
    use strict; use warnings; use Storable; my %hash; $hash{"Janet Jones"} = { "spouse" => "Bob Jones", "son 1" => "Tom Jones", "son 2" => "Pete Jones", "pet 1" => "Boyle", "father" => "Richard Smith", "mother" => "Fran Smith", "sister 1" => "Louise Williams", "address" => "1 Main St, Littleton, PA, 55555", }; $hash{"Bob Jones"} = { "spouse" => "Janet Jones", "son 1" => "Tom Jones", "son 2" => "Pete Jones", "pet 1" => "Boyle", "father" => "Paul Jones", "mother" => "Stella Jones", "brother 1" => "Paul Jones, Jr", "brother 2" => "Vince Jones", "brother 3" => "Dan Jones", "sister 1" => "Andrea Limux", "address" => "1 Main St, Littleton, PA, 55555", }; store(\%hash, "HashOfHashesFile.txt"); my %friends = %{retrieve("HashOfHashesFile.txt") }; for my $name (keys %friends){ print "$name->\n"; for my $info(keys %{$friends{$name}}){ print " " x length("$name"); print "$info-> $friends{$name}{$info}\n"; } }
    OUTPUT:
    Janet Jones-> spouse-> Bob Jones son 1-> Tom Jones pet 1-> Boyle mother-> Fran Smith father-> Richard Smith address-> 1 Main St, Littleton, PA, 55555 son 2-> Pete Jones sister 1-> Louise Williams Bob Jones-> spouse-> Janet Jones son 1-> Tom Jones brother 3-> Dan Jones pet 1-> Boyle mother-> Stella Jones brother 2-> Vince Jones father-> Paul Jones brother 1-> Paul Jones, Jr address-> 1 Main St, Littleton, PA, 55555 sister 1-> Andrea Limux son 2-> Pete Jones
    This link to a Wikipedia article is very informative of the concept of data serialization..


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
      Thank you very much for the responses. I have a question about the module Storable, though. Storable just unloads content to a file, while keeping information in memory, true? What if one wanted to use a file to use the file for the hash of hashes rather than using memory? The method you outlined is one method for handling persistency. I will be examining DBM::Deep. Thanks.

      Thanks.