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