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

Dear Monks

I tried to write a package which is used like
my $h2f = new MyPackage() ; $h2f->{fsize} = 1000 ; print "filename = " . $h2f->{fname} ; $h2f->cleanup() ; ....
What this package must do is
1) store / retrieve key-values from a file
2) modify (if necessary ) values STOREd or FETCHed

Here is my code so far
#! /usr/bin/perl -l use strict ; use warnings ; use Data::Dumper; package NewStdHash; use Tie::Hash; use Fcntl qw(:DEFAULT :flock) ; # For O_RDWR, O_CREAT, etc. use NDBM_File ; our @ISA = qw(Tie::StdHash); $| = 1 ; sub new { my $class = shift ; my $self = bless {}, ref $class || $class ; return $self->_init( @_ ) ; } sub _init { my $self = shift ; tie %$self, 'NewStdHash' ; my %storage = () ; tie %storage, 'NDBM_File', "demodata",O_RDWR|O_CREAT,0777 or die "$! +"; $self->{storage} = \%storage ; return $self ; } sub cleanup {} sub FETCH { my ( $self, $key ) = @_ ; # do some stuff return $self->{storage}->{$key} ; } sub STORE { my ( $self, $key, $value ) = @_ ; # do some stuff $self->{storage}->{$key} = $value ; } sub DESTROY { my $self = shift ; print "DESTORY" ; untie($self->{storage}) ; untie($self) ; } package main ; my $mh = new NewStdHash() ; print "val is " . $mh->{abc} ; $mh->{abc} = 20 ;
Unfortunately, it doesn't store or retrieve anything from the file. The only solution so far was to store the NDBM_File TIEd hash in a global variable, but I prefer storing it in $self!
Is it the TIEd hash inside a TIEd hash that causes the problem or something else. Any suggestions ?

cheers

Replies are listed 'Best First'.
Re: TIEd object (TIEd hash inside a TIEd hash)
by biohisham (Priest) on Jan 24, 2010 at 06:03 UTC
    There are many issues with the code you posted, there are methods which are missing, these methods are peculiar to tying hashes, in addition you have added a method "new" instead of "TIEHASH" as your class constructor...TIEHAHS would let you return a blessed reference through which the new object would be accessed but in a capacity related to hashes.

    Tying hashes is different from tying scalars or arrays since each have their own particular methods, and these are the methods that you need to define when it comes to tying a hash:

    • "TIEHASH" as a class constructor.
    • "FETCH" and "STORE" access the key/value pairs and this is where you can process or modify the values passed/tied.
    • "EXISTS" to check a key presence in a hash.
    • "DELETE" deletes a key-value pair.
    • "CLEAR" to purge the hash.
    • "FIRSTKEY" and "NEXTKEY" iterate over the key/value pairs when you call keys, values, or each. And as usual.
    • "Destroy" which maybe defined in special circumstances like when you want to deallocate resources.

    The documentation provides a straightforward approach, this topic is addressed in Programming Perl as well...


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
      Doesn't Tie::StdHash take care of all the missing methods ?
      Furthermore, I have a new method because in the main program I want to create an object with new, not with tie

      UPDATE: I don't think there are problems with my TIEd hashes. I get problems when I put the second one into the first
Re: TIEd object (TIEd hash inside a TIEd hash)
by Anonymous Monk on Jan 24, 2010 at 05:12 UTC
    What did you try?
    What happened?
    What did you expect to happen?
      what? did you read my post ?