| 1 | package Tie::Hash::CustomStorage ; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use vars qw($VERSION) ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | $VERSION = sprintf "%d.%03d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use base qw/Tie::Hash/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # if not init or tie_hash or init parameter is given, behaves exactly | ||||||||||||||||||||||||||||
| 13 | # as a standard hash | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | sub TIEHASH | ||||||||||||||||||||||||||||
| 16 | 5 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=11 SCAR=24.2
| |||||||||||||||||||||||||||||
| 17 | 5 | my $type = shift ; | |||||||||||||||||||||||||||
| 18 | 5 | my %args = @_ ; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 5 | my %data = () ; | |||||||||||||||||||||||||||
| 21 | 5 | my $autovivify = 1 ; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 5 | my $self = { data => \%data } ; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | #print "Creating Tie::Hash with '",join("','", %args),"'\n"; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | my $load = sub | ||||||||||||||||||||||||||||
| 28 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 29 | 4 | my $file = $_[0].'.pm'; | |||||||||||||||||||||||||||
| 30 | 4 | $file =~ s!::!/!g; | |||||||||||||||||||||||||||
| 31 | 4 | ▶ | require $file unless defined *{$_[0].'::'} ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 5 | }; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 5 | ▶ | if (defined $args{autovivify}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | { | ||||||||||||||||||||||||||||
| 36 | 0 | $autovivify = delete $args{autovivify} ; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # applied to hash containing the storage | ||||||||||||||||||||||||||||
| 40 | 5 | ▶ | if (defined $args{tie_hash}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | { | ||||||||||||||||||||||||||||
| 42 | 1 | my $p = delete $args{tie_hash} ; | |||||||||||||||||||||||||||
| 43 | 1 | ▶ | my ($class, @args) = ref $p ? @$p : ($p) ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 1 | $load->($class) ; | |||||||||||||||||||||||||||
| 45 | 1 | $self->{tie_hash_obj} = tie %data, $class, @args ; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 5 | my $init_obj = delete $args{init_object} ; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # applied to storage | ||||||||||||||||||||||||||||
| 51 | 5 | ▶ | if (defined $args{class_storage}) | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 52 | { | ||||||||||||||||||||||||||||
| 53 | 1 | my $p = delete $args{class_storage} ; | |||||||||||||||||||||||||||
| 54 | 1 | ▶ | my ($class, @args) = ref $p ? @$p : ($p) ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 1 | $load->($class) ; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | $self->{init} = $autovivify ? | ||||||||||||||||||||||||||||
| 58 | sub | ||||||||||||||||||||||||||||
| 59 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 60 | 2 | my $idx = shift ; | |||||||||||||||||||||||||||
| 61 | 2 | ▶ | my $obj = shift || $class -> new (@args) ; | ||||||||||||||||||||||||||
|
Condition66%2/3
shift() || $class->new(@args)
MC/DC0%0/2
shift() || $class->new(@args)
shift()
$class->new(@args)
Truth table66%2/3
A || B
A= shift()B= $class->new(@args)
| |||||||||||||||||||||||||||||
| 62 | 2 | ▶ | $init_obj->($obj,$idx) if defined $init_obj ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | $self->{data}{$idx} = $obj ; | |||||||||||||||||||||||||||
| 65 | 1 | ▶ | } : sub {} ; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | $self->{class_storage} = $class ; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | elsif (defined $args{tie_storage}) | ||||||||||||||||||||||||||||
| 70 | { | ||||||||||||||||||||||||||||
| 71 | 2 | my $p = delete $args{tie_storage} ; | |||||||||||||||||||||||||||
| 72 | 2 | ▶ | my ($class, @args) = ref $p ? @$p : ($p) ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 2 | $load->($class) ; | |||||||||||||||||||||||||||
| 74 | $self->{init} = sub | ||||||||||||||||||||||||||||
| 75 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 76 | #print "storage init with tie_storage\n"; | ||||||||||||||||||||||||||||
| 77 | 4 | my $ref = $self->get_storage_ref($_[0]) ; | |||||||||||||||||||||||||||
| 78 | 4 | my $obj = tie $$ref, $class, @args ; | |||||||||||||||||||||||||||
| 79 | 4 | ▶ | $init_obj->($obj,$_[0]) if defined $init_obj ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 2 | } ; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | elsif (defined $args{init_storage}) | ||||||||||||||||||||||||||||
| 83 | { | ||||||||||||||||||||||||||||
| 84 | 1 | my ($init_method, @args) = @{delete $args{init_storage}} ; | |||||||||||||||||||||||||||
| 85 | $self->{init} = sub | ||||||||||||||||||||||||||||
| 86 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | #print "storage init with init\n"; | ||||||||||||||||||||||||||||
| 88 | 2 | my $ref = $self->get_storage_ref($_[0]) ; | |||||||||||||||||||||||||||
| 89 | 2 | $init_method->($ref, @args) | |||||||||||||||||||||||||||
| 90 | 1 | } ; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | else | ||||||||||||||||||||||||||||
| 93 | { | ||||||||||||||||||||||||||||
| 94 | 1 | ▶ | $self->{init} = sub {} ; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 5 | ▶ | croak __PACKAGE__,": Unexpected TIEHASH argument: ", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | join(' ',keys %args) if %args ; | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 5 | bless $self, $type ; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # this one is tricky, all direct method calls to this class must be | ||||||||||||||||||||||||||||
| 104 | # forwarded to the tied object hidden behind the %data hash | ||||||||||||||||||||||||||||
| 105 | sub AUTOLOAD | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 107 | 0 | our $AUTOLOAD ; | |||||||||||||||||||||||||||
| 108 | 0 | my $self=shift ; | |||||||||||||||||||||||||||
| 109 | 0 | my $obj = $self->{tie_hash_obj} ; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if (defined $obj) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | { | ||||||||||||||||||||||||||||
| 113 | 0 | my ($pack,$method) = ($AUTOLOAD =~ /(.*)::(\w+)/) ; | |||||||||||||||||||||||||||
| 114 | 0 | $obj->$method(@_) ; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | else | ||||||||||||||||||||||||||||
| 117 | { | ||||||||||||||||||||||||||||
| 118 | 0 | croak "Undefined subroutine $AUTOLOAD called"; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | sub FETCH | ||||||||||||||||||||||||||||
| 123 | 12 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 124 | 12 | my ($self,$idx) = @_ ; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | #print "TieHash: fetch idx $idx\n"; | ||||||||||||||||||||||||||||
| 127 | 12 | ▶ | $self->{init}->($idx) unless defined $self->{data}{$idx} ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 12 | return $self->{data}{$idx} ; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # Implementation note: The tie must be applied to the variable which | ||||||||||||||||||||||||||||
| 133 | # is actually stored. For a standard hash this variable is | ||||||||||||||||||||||||||||
| 134 | # $self->{$name}{$key}. For a tied hash, the actual variable is hidden | ||||||||||||||||||||||||||||
| 135 | # within the tied hash. The code will find the actual location if the | ||||||||||||||||||||||||||||
| 136 | # tied hash inherits from StdHash or if the tied hash follows the | ||||||||||||||||||||||||||||
| 137 | # example of the camel book (e.g. $self->{DATA} or $self->{data}). If | ||||||||||||||||||||||||||||
| 138 | # all fails, the user's tied hash must provide a get_data_ref method | ||||||||||||||||||||||||||||
| 139 | # that give a ref to the actual location of the variable to be tied. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | sub get_storage_ref | ||||||||||||||||||||||||||||
| 142 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get_storage_ref: called CC=6 SCAR=29.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 143 | 7 | my ($self,$idx) = @_ ; | |||||||||||||||||||||||||||
| 144 | 7 | my $h_obj = $self->get_user_tied_hash_object ; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 7 | ▶ | return \$self->{data}{$idx} unless defined $h_obj; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # print "get_scalar_ref called for $h_obj,$idx\n"; | ||||||||||||||||||||||||||||
| 149 | return $h_obj->isa('Tie::StdHash') ? \$h_obj->{$idx} : | ||||||||||||||||||||||||||||
| 150 | defined $h_obj->{DATA} ? \$h_obj->{DATA}{$idx} : | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | defined $h_obj->{data} ? \$h_obj->{data}{$idx} : | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 152 | $h_obj->can('get_data_ref') ? $h_obj->get_data_ref($idx): | ||||||||||||||||||||||||||||
| 153 | die ref($h_obj)," must provide a get_data_ref method" ; | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | sub get_user_tied_hash_object | ||||||||||||||||||||||||||||
| 157 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get_user_tied_hash_object: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 158 | 7 | my $self = shift ; | |||||||||||||||||||||||||||
| 159 | 7 | return tied %{$self->{data}} ; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | sub get_tied_storage_object | ||||||||||||||||||||||||||||
| 163 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get_tied_storage_object: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 164 | 1 | my ($self,$idx) = @_ ; | |||||||||||||||||||||||||||
| 165 | 1 | ▶ | $self->{init}->($idx) unless defined $self->{data}{$idx} ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | #print "TieHash: get_storage on idx $idx\n"; | ||||||||||||||||||||||||||||
| 167 | 1 | my $r = $self->get_storage_ref($idx) ; | |||||||||||||||||||||||||||
| 168 | 1 | tied ($$r) ; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | sub STORE | ||||||||||||||||||||||||||||
| 172 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 173 | 7 | my ($self,$idx, $data) = @_ ; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | #print "TieHash: store idx $idx, data ", defined $data ? $data : 'UNDEF', " (", join('~',keys %{$self->{data}}),")\n"; | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 7 | my @args; | |||||||||||||||||||||||||||
| 178 | 7 | ▶ | if (defined $self->{class_storage} and defined $data) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"class_storage"} and defined $data
MC/DC50%1/2
defined $self->{"class_storage"} and defined $data
defined $self->{"class_storage"}
defined $data
Truth table66%2/3
A and B
A= defined $self->{"class_storage"}B= defined $data
| |||||||||||||||||||||||||||||
| 179 | { | ||||||||||||||||||||||||||||
| 180 | 2 | ▶ | if (ref($data) eq $self->{class_storage}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | { | ||||||||||||||||||||||||||||
| 182 | # provided object will be run through init process | ||||||||||||||||||||||||||||
| 183 | 1 | $self->{init}->($idx,$data) ; | |||||||||||||||||||||||||||
| 184 | 1 | return $self->{data}{$idx} ; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | else | ||||||||||||||||||||||||||||
| 187 | { | ||||||||||||||||||||||||||||
| 188 | 1 | croak ref($self),": wrong object assigned to index '$idx'. ", | |||||||||||||||||||||||||||
| 189 | "Expected '$self->{class_storage}', got '",ref($data),"'" ; | ||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 5 | ▶ | $self->{init}->($idx,$data) unless defined $self->{data}{$idx} ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 5 | return $self->{data}{$idx} = $data ; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 4 | ▶ | sub FIRSTKEY { my $a = scalar keys %{$_[0]->{data}}; each %{$_[0]->{data}} } | ||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 199 | 8 | ▶ | sub NEXTKEY { each %{$_[0]->{data}} } | ||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | sub STORESIZE | ||||||||||||||||||||||||||||
| 203 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
STORESIZE: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 204 | 0 | my ($self,$size) = @_ ; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 0 | ▶ | for (my $i = $#{$_[0]->{data}}; $i<$size; $i++) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | { | ||||||||||||||||||||||||||||
| 208 | 0 | $self->{init}->($i); | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 0 | ▶ | sub EXISTS { exists $_[0]->{data}{$_[1]} } | ||||||||||||||||||||||||||
|
Subroutine
EXISTS: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 213 | 0 | ▶ | sub DELETE { delete $_[0]->{data}{$_[1]} } | ||||||||||||||||||||||||||
|
Subroutine
DELETE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 214 | 0 | ▶ | sub CLEAR { %{$_[0]->{data}} = () } | ||||||||||||||||||||||||||
|
Subroutine
CLEAR: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 215 | 0 | ▶ | sub SCALAR { scalar %{$_[0]->{data}} } | ||||||||||||||||||||||||||
|
Subroutine
SCALAR: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 0 | ▶ | sub DESTROY {} | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 1 | 1; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | __END__ | ||||||||||||||||||||||||||||