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

When I uncomment lines 3-5, this code doesn't work. Why? When I uncomment the last 5 lines, it works. Is Storable the correct approach here? I'm going to write a hash to disk that will act like a miniature database. What should I read to learn more about persistent data structures?
use strict; my %testline; # use DB_File; # tie (%testline, 'DB_File', 'testline.db', O_RDWR|O_CREAT, # 0666, $DB_BTREE) or die "Cannot tie:$!"; push @{ $testline{a} }, { uri => 'http://', name => 'barney', state => 'MI' }; push @{ $testline{a} }, { uri => 'ftp://', name => 'jade', state => 'NM' }; # use Storable; # store \%testline, 'filetest'; # my $hashref = retrieve('filetest'); # use Data::Dumper; # print Data::Dumper->Dump([$hashref],['hashref']);

Replies are listed 'Best First'.
Re (tilly) 1: Limits of DB_File with Btree algorithm
by tilly (Archbishop) on Feb 05, 2001 at 22:14 UTC
    What gets stored is a stringified version of your data. Since references involve internal program state, it is not easy to transparently pass references (eg anon hashes) between programs. See MLDBM for a solution.
Re: Limits of DB_File with Btree algorithm
by runrig (Abbot) on Feb 05, 2001 at 22:12 UTC
    DB_File can not store complex data structures as values, only simple scalar values (i.e. strings, numbers) for each hash key. Therefore you can not store a hash of arrays of hashes. Unless you can serialize your value in some way (e.g. create a delimited string value out of the array of hashes that you're trying to store for each value), then Storable is the way to go.

    Update: Oh, neat, I didn't know about MLDBM, you learn something all the time around here :)