jeanluca has asked for the wisdom of the Perl Monks concerning the following question:
What this package must do ismy $h2f = new MyPackage() ; $h2f->{fsize} = 1000 ; print "filename = " . $h2f->{fname} ; $h2f->cleanup() ; ....
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!#! /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 ;
|
|---|
| 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 | |
by jeanluca (Deacon) on Jan 24, 2010 at 08:31 UTC | |
|
Re: TIEd object (TIEd hash inside a TIEd hash)
by Anonymous Monk on Jan 24, 2010 at 05:12 UTC | |
by jeanluca (Deacon) on Jan 24, 2010 at 10:28 UTC |