in reply to hashes frustration

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

nothing is brought back, why??????????
   I am a superhero.

Replies are listed 'Best First'.
RE: Re-post: hashes frustration
by Perlmage (Acolyte) on Sep 14, 2000 at 18:23 UTC

    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.

RE: Re-post: hashes frustration
by cwest (Friar) on Sep 14, 2000 at 17:42 UTC
    I haven't used DB_Files but this is my guess:


    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.

       I am a superhero.