1 package PLP::Tie::Delay;
9 Delays hash generation. Unties the hash on first access, and replaces it by the generated one.
10 Uses symbolic references, because circular ties make Perl go nuts :)
12 tie %Some::hash, 'PLP::Tie::Delay', 'Some::hash', sub { \%generated_hash };
14 This module is part of the PLP internals and probably not of any use to others.
20 untie %{ $self->[0] };
22 # I'd like to use *{ $self->[0] } = $self->[1]->(); here,
23 # but that causes all sorts of problems. The hash is accessible from
24 # within this sub, but not where its creation was triggered.
25 # Immediately after the triggering statement, the hash becomes available
26 # to all: even the scope where the previous access attempt failed.
28 %{ $self->[0] } = %{ $self->[1]->() }
32 # my ($class, $hash, $source) = @_;
33 return bless [ @_[1, 2] ], $_[0];
37 my ($self, $key) = @_;
39 return $self->[0]->{$key};
43 my ($self, $key, $value) = @_;
45 return $self->[0]->{$key} = $value;
49 my ($self, $key) = @_;
51 return delete $self->[0]->{$key};
57 return %{ $self->[0] };
61 my ($self, $key) = @_;
63 return exists $self->[0]->{$key};
73 # Let's hope this never happens. (It's shouldn't.)