in reply to TIEHASH hiccup?

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}; } Can't use an undefined value as a HASH reference at t2.pl line 27.<BR> +<BR> Why does pwotie's FETCH work ok with $o{0} called first, but fail when + $o{0}->{hi} is called first?

It doesn't work, because $o{0} is undefined, and you're trying to use that as a hash reference. Remember that when you use a tied hash, you can't count on complex vivification. You'll have to initialise a hash ref yourself:

sub FETCH { my ($this, $key) = @_; return $this->{l}{$key} if exists $this->{l}{$key}; # I don't know what you're doing, but eval is scary :) # Consider using a sub ref (sub { ... } or \&subname) instead $this->{l}{$key} = eval $this{f}{$key}; # Create a new hash ref if the eval returned undef $this->{l}{$key} = { } if not defined $this->{l}{$key}; return $this->{l}{$key}; }

U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk

Replies are listed 'Best First'.
Re: Re: TIEHASH hiccup?
by jhanna (Scribe) on Mar 19, 2002 at 20:33 UTC
    Right, well this would be true if $this->{l}{$key} were undefined (which it isn't). In fact, correct behavior for FETCH is to return undefined if $key doesn't reference a defined object.

    $o{0} should be defined because ->{f}{0} is defined.

    And the evals are just there to undo Data::Dump -- I'll probably switch to Storable later when all is more stable and I don't need to inspect my objects manually.

    Thanks for the stab at it, tho.