Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

hashes frustration

by ant (Scribe)
on Sep 14, 2000 at 16:20 UTC ( [id://32446]=perlquestion: print w/replies, xml ) Need Help??

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

I am currently reading about the Perl DBI module, and the first chapter explained about the Berkley DB_File and tieing hashes to a file. I followed the example from the book and succesfully got it working. However I then wrote a method that would return a particular part of a string thus (main parts of code) my $db = tie %database, 'DB_File', "simpleinsert.dat",O_CREAT | O_RDWR, 0666 or die "Cannot initialise database"; $database{'anthony1'}=new contacts_db('anthony', 'ant@hot', '14 road', '102892', '34567')->pack(); I can then access the name by my $person = new contacts_db($database{'anthony1'}); $person->unpack_name(); this then returns 'anthony'. but if i put $database{'anthony1'}->unpack_name(); nothing is brought back, why??????????

Replies are listed 'Best First'.
(jcwren) Re: hashes frustration
by jcwren (Prior) on Sep 14, 2000 at 18:07 UTC
    I haven't used DB_File, but I was playing with tied hashes in relation to Config::IniFiles and through a combination of not understanding the limitations of tied hashes and questionable documentation in the module, was having a problem.

    After corresponding with the author of the module, who had an incomplete picture of auto-vivification, he referred me to this link, and in particular Limitation #3. If you get to where you're trying to do something like this, perhaps this will save you some time.

    http://aut.dk/orqwood/dwh/dwh_pod.html

    (For all I know, this may be common knowledge. But it helped me, so pass it on I shall. Also, the module tis article refers to DWH_File seems like it could be useful.)

    --Chris

    e-mail jcwren
Re-post: hashes frustration
by cwest (Friar) on Sep 14, 2000 at 17:38 UTC
    I am currently reading about the Perl DBI module, and the first chapter explained about the Berkley DB_File and tieing hashes to a file. I followed the example from the book and succesfully got it working. However I then wrote a method that would return a particular part of a string thus (main parts of code)
    my $db = tie %database, 'DB_File', "simpleinsert.dat",O_CREAT | O_RDWR +, 0666 or die "Cannot initialise database"; $database{'anthony1'}=new contacts_db('anthony', 'ant@hot', '14 road', '102892', '34567')->pack();
    I can then access the name by
    my $person = new contacts_db($database{'anthony1'}); $person->unpack_name();
    this then returns 'anthony'.

    but if i put

    $database{'anthony1'}->unpack_name();
    nothing is brought back, why??????????
    --
    Casey
       I am a superhero.
    

      You're not putting an object in the hash, so you can't call a method on it later. What you're actually doing is calling a class method on the package that corresponds to whatever pack() returns.

      The first form works because you are re-blessing the packed version into the class that contains the unpack_name() method. I'm not sure why the second form works at all without resulting in an error, actually.

      There are modules on the CPAN that will let you tie() a hash to a database and then store and fetch references (including blessed ones) from them, or you could roll your own with Storable or FreezeThaw or the like.

      I haven't used DB_Files but this is my guess:

      try:

      (contacts_db($database->{anthony1}))->unpack_name()
      Since contacts_db() returns a reference for you to unpack_name() with, you can simplify this into one statement like the above.

      It's untested, but I'm 74.2% sure it will work.

      --
      Casey
         I am a superhero.
      

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-20 01:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found