On Thu, Mar 12, 2009 at 09:20, Jerry Rocteur <[email protected]> wrote:
> Hi,
>
> I'm trying to tie this kind of hash into SDBM
>
> $hash_of_baseline{$hdr_user_name} = { user_name => $hdr_user_name,
> passwd => $hdr_user_passwd,
> ...
> ...
> groups => [ @info_group_names ] };
>
> I can store it but when I read it back I get.
>
> Can't use string ("HASH(0x1db393b0)") as a HASH ref while "strict refs" in
> use at
>
> I think I understand from what I've googled that I can't use SDBM for this
> kind of structure.
>
> I searched around and found MLDBM.
>
> I'm looking for advice, is this the best solution or am I on the wrong track ?
snip
DBM files can only store simple values like strings and numbers. You
are trying to store a complex data structure in one. The general
method of solving this problem is to use the Storable module* to
serialize the data structure as a string before storing it in the DBM,
or to use a module like DBM::Deep** that automates this for you.
#!/usr/bin/perl
use strict;
use warnings;
use Fcntl; # For O_RDWR, O_CREAT, etc.
use SDBM_File;
use Storable qw/freeze thaw/;
use Data::Dumper;
tie my %dbm, 'SDBM_File', 'database.dbm', O_RDWR|O_CREAT, 0666
or die "could not tie SDBM database.dbm: $!\n";
my %deep = (
english => { one => 1, two => 2, three => 3 },
latin => { uno => 1, duos => 2, tres => 3 }
);
for my $key (keys %deep) {
$dbm{$key} = freeze $deep{$key};
}
my %new = map { $_ => thaw $dbm{$_} } keys %dbm;
print Dumper \%deep, \%new;
* http://perldoc.perl.org/Storable.html
** http://search.cpan.org/dist/DBM-Deep/lib/DBM/Deep.pod
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/