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;
use strict; use warnings; package Tie::LazyHash; require Tie::Hash; our @ISA = 'Tie::StdHash'; sub FETCH { Tie::LazyHash::Elem->new( @_ ); } package Tie::LazyHash::Elem; use Class::InsideOut qw(id private register); use Scalar::Util 'weaken'; use overload 'bool' => '_get'; use overload '""' => '_get'; use overload '0+' => '_get'; private _hash => my %hash; private _key => my %key; sub _hash; sub _key; sub new { my ($proto, $hash, $key) = @_; my $class = ref $proto || $proto; my $self = bless \do{ my $bidding }, $class; tie $$self, 'Tie::LazyHash::Elem::Inner', id($self); weaken( $hash{id $self} = $hash ); $key{id $self} = $key; register $self; } sub _get { my $self = shift; my $hash = _hash $self; return $hash->{_key $self}; } sub _set { my ($self, $val) = @_; my $hash = _hash $self; $hash->{_key $self} = $val; } sub delete { my $self = shift; my $hash = _hash $self; delete $hash->{_key $self}; } sub exists { my $self = shift; my $hash = _hash $self; exists $hash->{_key $self}; } sub _hash { my $self = shift; if (ref $self) { $self = id $self; } return $hash{$self}; } sub _key { my $self = shift; if (ref $self) { $self = id $self; } return $key{$self}; } package Tie::LazyHash::Elem::Inner; sub TIESCALAR { my ($class, $id) = @_; bless \$id, $class; } sub FETCH { my $self = shift; Tie::LazyHash::Elem::_get($$self); } sub STORE { my ($self, $val) = @_; Tie::LazyHash::Elem::_set($$self, $val); } 1; __END__

In reply to Re^6: Pronoun Programming by plobsing
in thread Pronoun Programming by rje

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.