1 package PLP::Tie::Delay;
8 Delays hash generation. Unties the hash on first access, and replaces it by the generated one.
9 Uses symbolic references, because circular ties make Perl go nuts :)
11 tie %Some::hash, 'PLP::Tie::Delay', 'Some::hash', sub { \%generated_hash };
13 This module is part of the PLP internals and probably not of any use to others.
19 untie %{ $self->[0] };
21 # I'd like to use *{ $self->[0] } = $self->[1]->(); here,
22 # but that causes all sorts of problems. The hash is accessible from
23 # within this sub, but not where its creation was triggered.
24 # Immediately after the triggering statement, the hash becomes available
25 # to all: even the scope where the previous access attempt failed.
27 %{ $self->[0] } = %{ $self->[1]->() }
31 # my ($class, $hash, $source) = @_;
32 return bless [ @_[1, 2] ], $_[0];
36 my ($self, $key) = @_;
38 return $self->[0]->{$key};
42 my ($self, $key, $value) = @_;
44 return $self->[0]->{$key} = $value;
48 my ($self, $key) = @_;
50 return delete $self->[0]->{$key};
56 return %{ $self->[0] };
60 my ($self, $key) = @_;
62 return exists $self->[0]->{$key};
72 # Let's hope this never happens. (It's shouldn't.)