in reply to Re^3: Pronoun Programming
in thread Pronoun Programming

I tried implementing this out of curiousity. HE* DO have a pointer back to the original hash, but there's no guaranteeing that the hash is there any more.

Thats why I tried using weakened references in stead. I would have used set/get magic if I was more comfortable with them, I never got those to work out for me before. Here's the begginings of the module (untested):
package Tie::BUKHash; sub TIEHASH { bless {}, ref $_[0] or $_[0]; } sub FETCH { my ($this, $key) = @_; return new Tie::BUKHash::Elem( $this, $key ); } sub STORE { my ($this, $key, $value) = @_; $this->{$key} = $value; } package Tie::BUKHash::Elem; use WeakRef 'weaken'; use Carp 'croak'; use constant HASH => 0; use constant KEY => 1; sub new { my ($proto, $hash, $key) = @_; my $self = bless [weaken($hash), $key], ref $proto or $proto; } sub get { my $self = shift; my $hash = $self->gethash; return $hash->{$self->[KEY]}; } sub set { my ($self, $val) = @_; my $hash = $self->gethash; $hash->{$self->[KEY]} = $val; } sub delete { my $self = shift; my $hash = $self->gethash; delete $hash->{$self->[KEY]}; } sub gethash { my $self = shift; return $self->[HASH] or croak "You shouldn't keep hash entries around after their respe +ctive hashes are gone!" ; }

Replies are listed 'Best First'.
Re^5: Pronoun Programming
by BrowserUk (Patriarch) on Feb 06, 2008 at 12:02 UTC
      I was mistaken. Should have paid a little more attention while reviewing extending and embedding perl. $hash in the following structure refers to the hashed value of the key, not the hashtable from which the HE came. Confusing terminology.
      # from Extending and Embedding Perl, section 4.5 $HE = { NEXT => $HE_next, HEK => { HASH => $hash, LEN => length($key), KEY => $key, } VAL => $SV, };
      My approach didn't try ordering, so the hash was actually based on a hashref, not an array.

      I looked over the tie modules on CPAN (the first 20 pages before I got bored). None of them seemed to implement the functionality of what I envisioned as a solution: A tied hash that gives defered processing objects as results.

      Being interested in something in inverse proportion to how useful that thing is, I expanded upon my ideas in the previous post. I think it caught feauture-use-itis though (2 tie classes and an overloaded inside-out class is a bit much complication for one module, IMHO).

      I called it Tie::LazyHash, but it probably would be better named Tie::DeferredLookupRefHash. But that's just ugly sounding.

      You can do a couple of things with the lookup values that invoke the actual lookup (which can be invoked multiple times for the same object):
      $elem->delete; $elem->exists; $$elem = $x; print $elem; # overloaded for convenience print $$elem;