| 1 | package Data::Lazy; | ||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 4 | $VERSION='0.6'; | |||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 4 | require Tie::Scalar; | |||||||||||||||||||||||||||||||
| 6 | 4 | require Exporter; | |||||||||||||||||||||||||||||||
| 7 | 4 | @ISA=qw(Exporter Tie::Scalar); | |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 4 | @EXPORT = qw(LAZY_STOREVALUE LAZY_STORECODE LAZY_READONLY); | |||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | sub LAZY_STOREVALUE () {0} | ||||||||||||||||||||||||||||||||
| 14 | sub LAZY_STORECODE () {1} | ||||||||||||||||||||||||||||||||
| 15 | sub LAZY_READONLY () {2} | ||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub LAZY_UNTIE () {croak "pass reference to tied var, not LAZY_UNTIE"} | ||||||||||||||||||||||||||||||
|
Subroutine
LAZY_UNTIE: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 8 | ▶ | sub TIESCALAR { | ||||||||||||||||||||||||||||||
|
Subroutine
TIESCALAR: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 21 | 8 | my $pack = shift; | |||||||||||||||||||||||||||||||
| 22 | 8 | my $self = {}; | |||||||||||||||||||||||||||||||
| 23 | 8 | $self->{code} = shift; | |||||||||||||||||||||||||||||||
| 24 | 8 | ▶ | $self->{'store'} = $_[0] if $_[0]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 25 | 8 | $self->{'type'} = 0; | |||||||||||||||||||||||||||||||
| 26 | 8 | bless $self => $pack; # That's it? Yup! | |||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 3 | ▶ | sub TIEARRAY { | ||||||||||||||||||||||||||||||
|
Subroutine
TIEARRAY: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 30 | 3 | my $pack = shift; | |||||||||||||||||||||||||||||||
| 31 | 3 | my $self = {}; | |||||||||||||||||||||||||||||||
| 32 | 3 | $self->{code} = shift; | |||||||||||||||||||||||||||||||
| 33 | 3 | ▶ | $self->{'store'} = $_[0] if $_[0]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 34 | 3 | $self->{'type'} = 1; | |||||||||||||||||||||||||||||||
| 35 | 3 | $self->{'size'} = 0; | |||||||||||||||||||||||||||||||
| 36 | 3 | bless $self => $pack; # That's it? Yup! | |||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 3 | ▶ | sub FETCHSIZE { | ||||||||||||||||||||||||||||||
|
Subroutine
FETCHSIZE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 40 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||
| 41 | 3 | return $self->{'size'}; | |||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 1 | ▶ | sub TIEHASH { | ||||||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 45 | 1 | my $pack = shift; | |||||||||||||||||||||||||||||||
| 46 | 1 | my $self = {}; | |||||||||||||||||||||||||||||||
| 47 | 1 | $self->{code} = shift; | |||||||||||||||||||||||||||||||
| 48 | 1 | ▶ | $self->{'store'} = $_[0] if $_[0]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | 1 | $self->{'type'} = 2; | |||||||||||||||||||||||||||||||
| 50 | 1 | ${$self->{'value'}}{$;} = $self->{code}; | |||||||||||||||||||||||||||||||
| 51 | 1 | bless $self => $pack; # That's it? Yup! | |||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 49 | ▶ | sub FETCH { | ||||||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=12 SCAR=25.2
| |||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 49 | my $self = shift; | |||||||||||||||||||||||||||||||
| 57 | 49 | ▶ | if ($self->{'type'} == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 58 | # scalar | ||||||||||||||||||||||||||||||||
| 59 | 11 | ▶ | return $self->{value} if exists $self->{value}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 60 | 8 | ▶ | if (ref $self->{code} eq 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | 7 | $self->{value} = &{$self->{code}}; | |||||||||||||||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||||||||||||||
| 63 | 1 | $self->{value} = eval $self->{code}; | |||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | 8 | ▶ | if (ref $self->{store}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | 1 | untie(${ delete $self->{store} }); | |||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | 8 | $self->{value}; | |||||||||||||||||||||||||||||||
| 69 | } elsif ($self->{'type'} == 1) { | ||||||||||||||||||||||||||||||||
| 70 | # array | ||||||||||||||||||||||||||||||||
| 71 | 34 | ▶ | if ($_[0] < 0) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 72 | 0 | $_[0] %= $self->{'size'} | |||||||||||||||||||||||||||||||
| 73 | } elsif ($_[0] - $self->{'size'} >= 0) { | ||||||||||||||||||||||||||||||||
| 74 | 5 | $self->{'size'} = $_[0]+1; | |||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | 34 | ▶ | return ${$self->{'value'}}[$_[0]] if defined ${$self->{'value'}}[$_[0]]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | 20 | ▶ | if (ref $self->{code} eq 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 78 | 20 | ${$self->{'value'}}[$_[0]] = &{$self->{code}}(@_); | |||||||||||||||||||||||||||||||
| 79 | } else { | ||||||||||||||||||||||||||||||||
| 80 | 0 | ${$self->{'value'}}[$_[0]] = eval $self->{code}; | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | 20 | ${$self->{'value'}}[$_[0]]; | |||||||||||||||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||||||||||||||
| 84 | # hash | ||||||||||||||||||||||||||||||||
| 85 | 4 | ▶ | unless (exists ${$self->{'value'}}{$_[0]}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 86 | 3 | ▶ | if (ref $self->{code} eq 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 87 | 3 | ${$self->{'value'}}{$_[0]} = &{$self->{code}}(@_); | |||||||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||||||
| 89 | 0 | ${$self->{'value'}}{$_[0]} = eval $self->{code}; | |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | 4 | ${$self->{'value'}}{$_[0]}; | |||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | 7 | ▶ | sub STORE { | ||||||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=9 SCAR=22.9
| |||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||
| 99 | 7 | ▶ | if ($self->{'type'} == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 100 | 5 | ▶ | if ($self->{'store'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | 5 | delete $self->{value}; | |||||||||||||||||||||||||||||||
| 103 | 5 | ▶ | if (defined $_[0]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 104 | 3 | ▶ | if ($self->{'store'} == LAZY_READONLY) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 105 | 1 | croak "Modification of a read-only value attempted"; | |||||||||||||||||||||||||||||||
| 106 | } elsif (ref $self->{store}) { | ||||||||||||||||||||||||||||||||
| 107 | # LAZY_UNTIE | ||||||||||||||||||||||||||||||||
| 108 | 1 | untie(${ delete $self->{store} }); | |||||||||||||||||||||||||||||||
| 109 | 1 | return shift; | |||||||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||||||
| 111 | # $self->{'store'} == LAZY_STORECODE | ||||||||||||||||||||||||||||||||
| 112 | 1 | $self->{code} = $_[0]; | |||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | } else { | ||||||||||||||||||||||||||||||||
| 116 | 0 | $self->{value} = $_[0]; | |||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||
| 118 | } elsif ($self->{'type'} == 1) { | ||||||||||||||||||||||||||||||||
| 119 | 1 | ▶ | if ($_[0] - $self->{'size'} >= 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | 1 | $self->{'size'} = $_[0]+1; | |||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | 1 | ${$self->{'value'}}[$_[0]] = $_[1]; | |||||||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||||||
| 124 | 1 | ▶ | if ($_[0] eq $;) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 125 | 0 | %{$self->{'value'}} = (); | |||||||||||||||||||||||||||||||
| 126 | 0 | $self->{'code'} = $_[1]; | |||||||||||||||||||||||||||||||
| 127 | 0 | ${$self->{'value'}}{$;} = $self->{code}; | |||||||||||||||||||||||||||||||
| 128 | } else { | ||||||||||||||||||||||||||||||||
| 129 | 1 | ${$self->{'value'}}{$_[0]} = $_[1]; | |||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub EXISTS {1} | ||||||||||||||||||||||||||||||
|
Subroutine
EXISTS: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | sub DELETE {undef} | ||||||||||||||||||||||||||||||
|
Subroutine
DELETE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub CLEAR {%{$_[0]->{'value'}} = ()} | ||||||||||||||||||||||||||||||
|
Subroutine
CLEAR: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | sub FIRSTKEY { | ||||||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 141 | 0 | my ($key,$val) = each %{$_[0]->{'value'}}; | |||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | ($key,$val) = each %{$_[0]->{'value'}}if ($key eq $;); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 143 | 0 | $key | |||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | sub NEXTKEY { | ||||||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 146 | 0 | my ($key,$val) = each %{$_[0]->{'value'}}; | |||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | ($key,$val) = each %{$_[0]->{'value'}}if ($key eq $;); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 148 | 0 | $key | |||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 152 | 10 | ▶ | sub import { | ||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=7 SCAR=20.0
| |||||||||||||||||||||||||||||||||
| 153 | 10 | my $caller_pack = caller; | |||||||||||||||||||||||||||||||
| 154 | 10 | my $my_pack = shift; | |||||||||||||||||||||||||||||||
| 155 | # print STDERR "exporter args: (@_); caller pack: $caller_pack\n"; | ||||||||||||||||||||||||||||||||
| 156 | # if (@_ % 2) { | ||||||||||||||||||||||||||||||||
| 157 | # croak "Argument list in `use $my_pack' must be list of pairs; aborting"; | ||||||||||||||||||||||||||||||||
| 158 | # } | ||||||||||||||||||||||||||||||||
| 159 | 10 | ▶ | while (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 160 | 4 | my $varname = shift; | |||||||||||||||||||||||||||||||
| 161 | 4 | my $function = shift; | |||||||||||||||||||||||||||||||
| 162 | 4 | ▶ | my $store = (($_[0] and $_[0] =~ /^[012]$/) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$_[0] and $_[0] =~ /^[012]$/
MC/DC0%0/2
$_[0] and $_[0] =~ /^[012]$/
$_[0]
$_[0] =~ /^[012]$/
Truth table66%2/3
A and B
A= $_[0]B= $_[0] =~ /^[012]$/
| |||||||||||||||||||||||||||||||||
| 163 | ? shift | ||||||||||||||||||||||||||||||||
| 164 | : ($function | ||||||||||||||||||||||||||||||||
| 165 | ? LAZY_STOREVALUE | ||||||||||||||||||||||||||||||||
| 166 | : LAZY_STORECODE)); | ||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | 4 | ▶ | if ($varname =~ /^\%(.*)$/) { #<???> | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 169 | 0 | my %fakehash; | |||||||||||||||||||||||||||||||
| 170 | 0 | tie %fakehash, $my_pack, $function, $store; #<???> | |||||||||||||||||||||||||||||||
| 171 | 0 | *{$caller_pack . '::' . $1} = \%fakehash; | |||||||||||||||||||||||||||||||
| 172 | } elsif ($varname =~ /^\@(.*)$/) { #<???> | ||||||||||||||||||||||||||||||||
| 173 | 1 | my @fakearray; | |||||||||||||||||||||||||||||||
| 174 | 1 | tie @fakearray, $my_pack, $function, $store; #<???> | |||||||||||||||||||||||||||||||
| 175 | 1 | *{$caller_pack . '::' . $1} = \@fakearray; | |||||||||||||||||||||||||||||||
| 176 | } else { | ||||||||||||||||||||||||||||||||
| 177 | 3 | $varname =~ s/^\$//; | |||||||||||||||||||||||||||||||
| 178 | 3 | my $fakescalar; | |||||||||||||||||||||||||||||||
| 179 | 3 | tie $fakescalar, $my_pack, $function, $store; #<???> | |||||||||||||||||||||||||||||||
| 180 | 3 | *{$caller_pack . '::' . $varname} = \$fakescalar; | |||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||
| 183 | 10 | @_ = ($my_pack); | |||||||||||||||||||||||||||||||
| 184 | 10 | goto &Exporter::import; | |||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||
| 186 | 4 | ▶ | use strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | 4 | 1; | |||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||
| 190 | __END__ | ||||||||||||||||||||||||||||||||