Array/CustomStorage.pm

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