File Coverage

blib/lib/PLP/Tie/Delay.pm
Criterion Covered Total %
statement 10 38 26.3
branch n/a
condition n/a
subroutine 4 14 28.5
pod n/a
total 14 52 26.9


line stmt bran cond sub pod time code
1             package PLP::Tie::Delay;
2              
3 1     1   6 use strict;
  1         2  
  1         46  
4 1     1   5 no strict 'refs';
  1         2  
  1         29  
5 1     1   5 use warnings;
  1         2  
  1         539  
6              
7             our $VERSION = '1.00';
8              
9             =head1 PLP::Tie::Delay
10              
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 :)
13              
14             tie %Some::hash, 'PLP::Tie::Delay', 'Some::hash', sub { \%generated_hash };
15              
16             This module is part of the PLP internals and probably not of any use to others.
17              
18             =cut
19              
20             sub _replace {
21 0     0   0 my ($self) = @_;
22 0         0 untie %{ $self->[0] };
  0         0  
23              
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.
29            
30 0         0 %{ $self->[0] } = %{ $self->[1]->() }
  0         0  
  0         0  
31             }
32              
33             sub TIEHASH {
34             # my ($class, $hash, $source) = @_;
35 36     36   169 return bless [ @_[1, 2] ], $_[0];
36             }
37              
38             sub FETCH {
39 0     0     my ($self, $key) = @_;
40 0           $self->_replace;
41 0           return $self->[0]->{$key};
42             }
43              
44             sub STORE {
45 0     0     my ($self, $key, $value) = @_;
46 0           $self->_replace;
47 0           return $self->[0]->{$key} = $value;
48             }
49              
50             sub DELETE {
51 0     0     my ($self, $key) = @_;
52 0           $self->_replace;
53 0           return delete $self->[0]->{$key};
54             }
55              
56             sub CLEAR {
57 0     0     my ($self) = @_;
58 0           $self->_replace;
59 0           return %{ $self->[0] };
  0            
60             }
61              
62             sub EXISTS {
63 0     0     my ($self, $key) = @_;
64 0           $self->_replace;
65 0           return exists $self->[0]->{$key};
66             }
67              
68             sub FIRSTKEY {
69 0     0     my ($self) = @_;
70 0           $self->_replace;
71 0           return 'PLPdummy';
72             }
73              
74             sub NEXTKEY {
75             # Let's hope this never happens. (It's shouldn't.)
76 0     0     return undef;
77             }
78              
79 0     0     sub UNTIE { }
80              
81 0     0     sub DESTROY { }
82              
83             1;
84