| 1 | package PLP::Tie::Delay; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | our $VERSION = '1.01'; | |||||||||
| 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 | 17 | ▶ | sub _replace { | ||||||||
|
Subroutine
_replace: called CC=3 SCAR=13.6
| |||||||||||
| 21 | 17 | my ($self) = @_; | |||||||||
| 22 | |||||||||||
| 23 | 17 | ▶ | if ($] >= 5.018) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 17 | ▶ | my $code = delete $self->[1] or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 5 | $self->[0] = $code->(); | |||||||||
| 26 | 5 | return 1; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 0 | untie %{ $self->[0] }; | |||||||||
| 30 | 0 | %{ $self->[0] } = %{ $self->[1]->() }; # *{ $self->[0] } = $self->[1]->(); | |||||||||
| 31 | 0 | return; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 46 | ▶ | sub TIEHASH { | ||||||||
|
Subroutine
TIEHASH: called CC=1 SCAR=0.0
| |||||||||||
| 35 | # my ($class, $hash, $source) = @_; | ||||||||||
| 36 | 46 | return bless [ @_[1, 2] ], $_[0]; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 7 | ▶ | sub FETCH { | ||||||||
|
Subroutine
FETCH: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 7 | my ($self, $key) = @_; | |||||||||
| 41 | 7 | $self->_replace; | |||||||||
| 42 | 7 | return $self->[0]->{$key}; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 1 | ▶ | sub STORE { | ||||||||
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||||||||||
| 46 | 1 | my ($self, $key, $value) = @_; | |||||||||
| 47 | 1 | $self->_replace; | |||||||||
| 48 | 1 | return $self->[0]->{$key} = $value; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | sub DELETE { | ||||||||
|
Subroutine
DELETE: not called CC=1 SCAR=6.9
| |||||||||||
| 52 | 0 | my ($self, $key) = @_; | |||||||||
| 53 | 0 | $self->_replace; | |||||||||
| 54 | 0 | return delete $self->[0]->{$key}; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 1 | ▶ | sub CLEAR { | ||||||||
|
Subroutine
CLEAR: called CC=1 SCAR=0.0
| |||||||||||
| 58 | 1 | my ($self) = @_; | |||||||||
| 59 | 1 | $self->_replace; | |||||||||
| 60 | 1 | return %{ $self->[0] }; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 0 | ▶ | sub EXISTS { | ||||||||
|
Subroutine
EXISTS: not called CC=1 SCAR=6.9
| |||||||||||
| 64 | 0 | my ($self, $key) = @_; | |||||||||
| 65 | 0 | $self->_replace; | |||||||||
| 66 | 0 | return exists $self->[0]->{$key}; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 8 | ▶ | sub FIRSTKEY { | ||||||||
|
Subroutine
FIRSTKEY: called CC=2 SCAR=6.9
| |||||||||||
| 70 | 8 | my ($self) = @_; | |||||||||
| 71 | 8 | ▶ | $self->_replace and return 'PLPdummy'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 4 | return each %{$self->[0]}; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 9 | ▶ | sub NEXTKEY { | ||||||||
|
Subroutine
NEXTKEY: called CC=1 SCAR=0.0
| |||||||||||
| 76 | 9 | return each %{$_[0]->[0]}; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 0 | ▶ | sub UNTIE { } | ||||||||
|
Subroutine
UNTIE: not called CC=1 SCAR=0.0
| |||||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | sub DESTROY { } | ||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||
| 82 | |||||||||||
| 83 | 2 | 1; | |||||||||
| 84 | |||||||||||