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

In reply to TIEd object (TIEd hash inside a TIEd hash) by jeanluca

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.