jhanna has asked for the wisdom of the Perl Monks concerning the following question:
Assume %l = { 0=>{oid=>0, hi=>there}, 1=>{oid=>1, hi=>woof}} and that %o is my TIE'd object store.
This works:
But this gives the following error:print "$o{0} $o{0}->{hi}\n";
Can't use an undefined value as a HASH reference at t2.pl line 27.print "$o{0}->{hi}\n";
Why does pwotie's FETCH work ok with $o{0} called first, but fail when $o{0}->{hi} is called first?
Here's my source for you to try yourself:
I'm using perl, v5.6.1 built for MSWin32-x86-multi-thread{ # implements persistence based on dirty and tied persistence in %o package Pwo; our %o; our $keep; sub flush { for (values %o) { if($_->{dirty}) { delete $_->{dirty}; $keep->keep($_); print "k: $_->{hi} "; }}} sub Pwo { my($class,$filename)=@_; $class=ref($class) || $class; $keep=tie(%o, pwotie, $filename) || die "Pwo: Persistence failed: ", +join(', ',@dbm)," -- error:$!\n"; my $self={ 'o'=>\%o, }; bless $self,$class; # if this is a new install we need to load the base objects print "o{0}=$o{0}\n"; bootstrap() unless defined $o{0}; print "o{0}=$o{0} 0=$o{0}->{hi} 1=$o{1}->{hi}\n"; return $self; } sub bootstrap { print "bs\n"; $o{0}={hi=>there, how=>'are you', dirty=>1, oid=>0}; $o{1}={hi=>"woof",dirty=>1,oid=>1}; } } { package pwotie; use Data::Dump qw(dump); use GDBM_File; sub TIEHASH { my ($class,$filename)=@_; $Result = tie(%f, 'GDBM_File', $filename, 2, 0640); ## duno what's wr +ong with my perl -- GDBM_WRCREAT=2 for me. if (! $Result){ print "\n\nCould not open GDBM file '$filename': $!\n"; } return bless { file=>$filename, f => \%f, l => () }, $class; } sub keep {$_[0]->{f}{$_[1]->{oid}}=dump($_[1]); print "sto: ".dump($_[ +1])."\n";} sub STORE { my ($this, $key, $value)=@_; return $this->{l}{$key}=($value); } sub FETCH { my ($this, $key)=@_; return $this->{'l'}{$key} if exists $this->{'l'}{$key}; print "trying f's $key: $this->{f}{$key}\n"; $this->{'l'}{$key}=eval($this->{f}{$key}); return $this->{'l'}{$key}; } sub FIRSTKEY { my $a = scalar keys %{$_[0]->{l}}; each %{$_[0]->{l}}; +} sub NEXTKEY { each %{$_[0]->{l}}; } sub EXISTS { my ($this, $key)=@_; return exists $this->{f}{$key};} sub DELETE {my ($this, $key)=@_; delete $this->{l}{$key}; delete $this->{f}{$key}; } sub CLEAR {my ($this)=@_; undef %{$this->{l}}; undef %{$this->{f}}; } } $p=Pwo->Pwo("tst.db" ); $p->flush();
Binary build 631 provided by ActiveState Tool Corp. http://www.ActiveState.com
Built 17:16:22 Jan 2 2002
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: TIEHASH hiccup?
by Juerd (Abbot) on Mar 19, 2002 at 06:23 UTC | |
by jhanna (Scribe) on Mar 19, 2002 at 20:33 UTC | |
Re: TIEHASH hiccup?
by rjray (Chaplain) on Mar 19, 2002 at 01:15 UTC | |
by jhanna (Scribe) on Mar 19, 2002 at 20:29 UTC | |
TIEHASH: Solved
by jhanna (Scribe) on Mar 19, 2002 at 21:54 UTC |