Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

dbmopen and hashes of hashes

by mulander (Monk)
on Oct 13, 2005 at 14:24 UTC ( [id://499908]=perlquestion: print w/replies, xml ) Need Help??

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

Hello fellow monks, I am trying to create a hash of keys from usernames, and each key would contain a hash of details. When I do it this way it works fine:
#!/usr/bin/perl use warnings; use strict; my %users; $users{mulander}->{email} = 'netprobe@gmail.com'; print "$users{mulander}->{email}\n";
I would like to store the data using dbmopen so I tried:
#!/usr/bin/perl use warnings; use strict; dbmopen my %users,'users',664; $users{mulander}->{email} = 'netprobe@gmail.com'; print "$users{mulander}->{email}\n"; dbmclose %users;
But when I do that, I keep getting this error:
Can't use string ("HASH(0x1865480)") as a HASH ref while "strict refs" + in use
I wonder why does this work fine when used on a ordinardy hash and fails when I open the hash via dbmopen? What am I doing wrong? Is a dbmopen hash different than an ordinary hash? And if so should I use another method to tie this data structure to a file? ( maybe Tie::Scalar or something? ).
I gone through perldoc and Beginning Perl and unfortunetly did not find the anwser, maybe one of you brothers could enlighten me.
Thanks in advance.

Replies are listed 'Best First'.
Re: dbmopen and hashes of hashes
by philcrow (Priest) on Oct 13, 2005 at 14:29 UTC
    dbm can't store anything but strings. So it converted your second level hash to a string. If you need to store things like this, consider combining Storable with dbmopen. First use freeze from Storable to turn the object into a string. Then store that via the dbm hash. Later, use thaw to get the data back.

    Phil

      One thing to be careful of (that bit me when I did this) is that some DBMs limit the length of string that they can store. I believe I ran into a 1024-character limit, which is clearly not all that long.

      You can certainly work around this; I ended up using two tables. The actual data is split up into 1000-character chunks and a separate table is used to index into that for reconstruction. A better solution would be DBM::Deep, which handles all of this for you. :-)

      Thank you, Storable is doing great! I wasn't even aware of it's existance and thanks to you I may consider my problem solved. Thanks again.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://499908]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-16 06:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found