blib/lib/Class/Data/TIN.pm

filter: Statementstmt 92.4% 159 / 172 Branchbran 67.5% 50 / 74 Conditioncond 76.3% 29 / 38 MC/DCmcdc 52.0% 13 / 25 Subroutinesub 100% 24 / 24 Podpod 80.0% 4 / 5 totaltotal 82.5% 279 / 338 CC 54 SCAR 22.2
CC
54
Cov
84%
_merge
21.3
_import_data
16.7
get_classdata
10.0
CRAP 9.2
↑ Summary
1 package Class::Data::TIN;
2  
3 # ABSTRACT: DEPRECATED - Translucent, Inheritable, Nonpolluting class data
4 1 our $VERSION = '0.03'; # VERSION
5  
6 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 warn __PACKAGE__ .' is DEPRECATED, please do not use this module anymore';
11  
12 1 ▶use Class::DispatchToAll qw(dispatch_to_all);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 require Exporter;
15  
16 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 our @ISA = qw(Exporter);
20 1 our @EXPORT_OK = ('get_classdata','set_classdata','append_classdata','merge_classdata');
21  
22 1 our $stop="_tinstop";
23  
24 # not exported, has to be called explicitly with Class::Data::TIN->new()
25 6 ▶sub new {
Subroutine new: called CC=7 SCAR=19.7
Pod documented
26 6 shift; # remove own ClassName 'Class::Data::TIN'
27 6 my $org_package=shift; # get name of package to store vars in
28 6 my $data=_import_data(@_);
29  
30 6 ▶ croak("data structure must be a hashref") if ($data && ref($data) ne "HASH");
Branch: 1 branch
BranchTrueFalseTotal
if $data and ref $data ne "HASH" 0 6 6
Condition33%1/3
$data and ref $data ne "HASH"
!ll&&!rl&&r
060
MC/DC0%0/2
$data and ref $data ne "HASH"
$data ref $data ne "HASH"
Truth table33%1/3
A and B
A= $dataB= ref $data ne "HASH"
ABresult
0X0
100
111
31  
32 6 my $tin_package=__PACKAGE__."::".$org_package;
33  
34 ### put data into TIN
35 # start eval-string
36 6 my $install="package $tin_package;";
37  
38 # add ISA's
39 6 my @isa=eval "@".$org_package."::ISA";
40 6 my @isa_tin;
41 6 foreach (@isa) {
42 5 push(@isa_tin,__PACKAGE__."::".$_);
43 }
44 6 ▶ $install.='our @ISA=(qw ('."@isa_tin".'));' if @isa_tin;
Branch: 1 branch
BranchTrueFalseTotal
if @isa_tin 4 2 6
45  
46 # $install.='$'.__PACKAGE__.'::_tin=$data;';
47 6 $install.='our $_tin=$data;';
48  
49 6 eval $install;
50 6 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
51  
52 # generate accessor methods in $tin_package
53 6 for my $key (keys %$data) {
54 9 _make_accessor($tin_package,$key);
55 }
56  
57 # return empty fake pseudo obj, to make calling get/set/append easier
58 # this is /not/ blessed, in fact, its just an alias to __PACKAGE__
59 6 return $org_package;
60 }
61  
62 # not exported
63 14 ▶sub _import_data {
Subroutine _import_data: called CC=7 SCAR=28.2
64 14 my $data;
65 14 ▶ if (@_ == 1) { # one param passed
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 10 4 14
66 10 my $param=shift;
67 10 ▶ if (ref($param) eq 'HASH') { # is it a HASH ref ?
Branch: 2 branches
BranchTrueFalseTotal
if (ref $param eq 'HASH') { } 10 0 10
elsif (-e $param) { } 0 0 0
68 10 $data=$param;
69 } elsif (-e $param) { # or is it a file ?
70 0 $data=do $param;
71 0 ▶ unless ($data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 0 0
72 0 ▶ croak("couldn't parse $param: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
73 0 ▶ croak("couldn't do $param: $!") unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
74 }
75 } else { # then something is wrong
76 0 croak("param is neither HASH REF nor file ...");
77 }
78 } else { # more params passed, treat as HASH
79 4 $data={@_};
80 }
81 14 return $data;
82 }
83  
84 52 ▶sub _save_val {
Subroutine _save_val: called CC=4 SCAR=13.9
85 52 my ($pkg,$key,$val,$stopper)=@_;
86  
87 # if (ref($val) eq "ARRAY" && $val->[0] eq $stop) {
88 # $val=$val->[1];
89 # }
90  
91 52 ▶ if ($stopper && $stopper>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stopper and $stopper > 0) 8 44 52
Condition100%3/3
$stopper and $stopper > 0
!ll&&!rl&&r
4318
MC/DC100%2/2
$stopper and $stopper > 0
$stopper $stopper > 0
Truth table100%3/3
A and B
A= $stopperB= $stopper > 0
ABresult
0X0
100
111
92 8 $val=[$stop,$val];
93 }
94  
95 52 my $install='$'.$pkg.'::_tin->{$key}=$val';
96 52 eval $install;
97 52 ▶ croak($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 52 52
98  
99 52 _make_accessor($pkg,$key);
100  
101 52 return;
102 }
103  
104 61 ▶sub _make_accessor {
Subroutine _make_accessor: called CC=3 SCAR=11.0
105 61 my ($pkg,$key)=@_;
106 1 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
107 61 my $accessor=$pkg."::".$key;
108 61 ▶ return if *$accessor{CODE}; # there is allready an accessor
Branch: 1 branch
BranchTrueFalseTotal
if *$accessor{"CODE"} 31 30 61
109  
110 30 my $r_tin=eval '$'."$pkg".'::_tin';
111 30 ▶ croak($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 30 30
112  
113 288 ▶ *$accessor = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
114 288 my $self=shift;
115 288 ▶ $r_tin->{$key} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 288 288
116 288 return $r_tin->{$key};
117 30 };
118 30 return;
119 }
120  
121 # ueberschreibt den aktuellen Wert in der package mit dem neuen
122 # geht mit einem wert
123 18 ▶sub set_classdata {
Subroutine set_classdata: called CC=2 SCAR=7.2
Pod documented
124 18 my ($self,$key,$val,$stopper)=@_;
125 18 ▶ my $package=ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
1800
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
126  
127 18 my $tin=__PACKAGE__."::".$package;
128 18 _save_val($tin,$key,$val,$stopper);
129 18 return $tin->$key();
130 }
131  
132 # haengt daten an die daten in der aktuellen package dran
133 # geht mit einem wert
134 # copy on write
135 15 ▶sub append_classdata {
Subroutine append_classdata: called CC=4 SCAR=13.9
Pod documented
136 15 my ($self,$key,$val,$stopper)=@_;
137 15 ▶ return unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 15 15
138 15 ▶ my $package=ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
1320
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
139  
140 # aktuellen wert hohlen
141 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
142 15 my $tin=__PACKAGE__."::".$package;
143 15 my $rtin=$tin."::_tin";
144 15 my $oldval=$$rtin->{$key};
145  
146 # neuen wert dranhaengen
147 15 ▶ if ($oldval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oldval) 13 2 15
148 13 ($val,$stopper)=_merge($oldval,$val,$stopper);
149 }
150  
151 15 _save_val($tin,$key,$val,$stopper);
152 15 return;
153 }
154  
155 # wie append, nur mit mehreren vals auf einmal
156 8 ▶sub merge_classdata {
Subroutine merge_classdata: called CC=5 SCAR=16.1
Pod undocumented
157 8 my $self=shift;
158 8 ▶ my $package=ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
440
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
159 8 my $data=_import_data(@_);
160  
161 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
162 8 my $tin=__PACKAGE__."::".$package;
163 8 my $rtin=$tin."::_tin";
164  
165 8 ▶ while (my ($key,$val)=each %$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$data) 19 8 27
166 19 my $stopper;
167 19 my $oldval=$$rtin->{$key};
168 19 ▶ if ($oldval && $val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oldval and $val) 4 15 19
Condition66%2/3
$oldval and $val
!ll&&!rl&&r
1504
MC/DC50%1/2
$oldval and $val
$oldval $val
Truth table66%2/3
A and B
A= $oldvalB= $val
ABresult
0X0
100
111
169 4 ($val,$stopper)=_merge($oldval,$val);
170 }
171 19 _save_val($tin,$key,$val,$stopper);
172 }
173 8 return;
174 }
175  
176 17 ▶sub _merge {
Subroutine _merge: called CC=16 SCAR=30.6
177 17 my ($oldval,$newval,$stopper)=@_;
178  
179 17 my $ref=ref($oldval);
180 17 my $refnew=ref($newval);
181  
182 17 ▶ if ($ref eq "ARRAY" && $oldval->[0] eq $stop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "ARRAY" and $oldval->[0] eq $Class::Data::TIN::stop) 4 13 17
Condition100%3/3
$ref eq "ARRAY" and $oldval->[0] eq $Class::Data::TIN::stop
!ll&&!rl&&r
854
MC/DC100%2/2
$ref eq "ARRAY" and $oldval->[0] eq $Class::Data::TIN::stop
$ref eq "ARRAY" $oldval->[0] eq $Class::Data::TIN::stop
Truth table100%3/3
A and B
A= $ref eq "ARRAY"B= $oldval->[0] eq $Class::Data::TIN::stop
ABresult
0X0
100
111
183 4 $oldval=$oldval->[1];
184 4 $ref=ref($oldval);
185 4 $stopper++;
186 }
187  
188 17 ▶ if ($refnew eq "ARRAY" && $newval->[0] eq $stop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refnew eq "ARRAY" and $newval->[0] eq $Class::Data::TIN::stop) 1 16 17
Condition100%3/3
$refnew eq "ARRAY" and $newval->[0] eq $Class::Data::TIN::stop
!ll&&!rl&&r
1061
MC/DC100%2/2
$refnew eq "ARRAY" and $newval->[0] eq $Class::Data::TIN::stop
$refnew eq "ARRAY" $newval->[0] eq $Class::Data::TIN::stop
Truth table100%3/3
A and B
A= $refnew eq "ARRAY"B= $newval->[0] eq $Class::Data::TIN::stop
ABresult
0X0
100
111
189 1 $newval=$newval->[1];
190 1 $refnew=ref($newval);
191 1 $stopper++;
192 }
193  
194 17 ▶ if (!$ref || $ref eq "SCALAR") {
Branch: 4 branches
BranchTrueFalseTotal
if (not $ref or $ref eq 'SCALAR') { } 4 13 17
elsif ($ref eq 'HASH') { } 6 7 13
elsif ($ref eq 'ARRAY') { } 7 0 7
elsif ($ref eq 'CODE') { } 0 0 0
Condition66%2/3
not $ref or $ref eq "SCALAR"
l!l&&r!l&&!r
4013
MC/DC50%1/2
not $ref or $ref eq "SCALAR"
not $ref $ref eq "SCALAR"
Truth table66%2/3
not(A) or B
A= not $refB= $ref eq "SCALAR"
ABresult
1X1
011
000
195 4 ▶ if ($refnew eq "SCALAR") {
Branch: 1 branch
BranchTrueFalseTotal
if ($refnew eq 'SCALAR') { } 1 3 4
196 1 $oldval=$newval;
197 } else {
198 3 ▶ if ($ref eq "SCALAR") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq 'SCALAR') { } 0 3 3
199 0 my $v=$$oldval;
200 0 $v.=$newval;
201 0 $oldval=\$v;
202 } else {
203 3 $oldval.=$newval;
204 }
205 }
206 } elsif ($ref eq "HASH") {
207 6 ▶ if (!$refnew) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $refnew) { } 0 6 6
elsif ($refnew eq 'HASH') { } 6 0 6
208 0 $oldval={%$oldval,$newval};
209 } elsif ($refnew eq "HASH") {
210 6 $oldval={%$oldval,%$newval};
211 } else {
212 0 croak("type mismatch!");
213 }
214 } elsif ($ref eq "ARRAY") {
215 7 ▶ if (!$refnew) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $refnew) { } 0 7 7
elsif ($refnew eq 'ARRAY') { } 7 0 7
216 0 push(@$oldval,$newval);
217 } elsif ($refnew eq "ARRAY") {
218 7 push(@$oldval,@$newval);
219 } else {
220 0 croak("type mismatch!");
221 }
222 } elsif ($ref eq "CODE") {
223 0 croak("cannot append/merge code ref");
224 }
225  
226 17 return ($oldval,$stopper);
227 }
228  
229  
230 84 ▶sub get_classdata {
Subroutine get_classdata: called CC=10 SCAR=23.0
Pod documented
231 84 my ($self,$key)=@_;
232 84 ▶ my $package=ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
8130
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
233 84 my $tin=__PACKAGE__."::".$package;
234  
235 84 my @vals=dispatch_to_all($tin,$key);
236 84 ▶ return unless @vals;
Branch: 1 branch
BranchTrueFalseTotal
unless @vals 2 82 84
237  
238 # peek at first val of @vals to decide data type
239 82 my $ref=ref($vals[0]);
240  
241 # check if stoptin caused wrong ref
242 82 ▶ if ($ref eq "ARRAY" && $vals[0]->[0] eq $stop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "ARRAY" and $vals[0][0] eq $Class::Data::TIN::stop) 9 73 82
Condition100%3/3
$ref eq "ARRAY" and $vals[0][0] eq $Class::Data::TIN::stop
!ll&&!rl&&r
54199
MC/DC100%2/2
$ref eq "ARRAY" and $vals[0][0] eq $Class::Data::TIN::stop
$ref eq "ARRAY" $vals[0][0] eq $Class::Data::TIN::stop
Truth table100%3/3
A and B
A= $ref eq "ARRAY"B= $vals[0][0] eq $Class::Data::TIN::stop
ABresult
0X0
100
111
243 9 $ref=ref($vals[0]->[1]);
244 }
245  
246 82 ▶ $ref||="SCALAR";
Condition100%2/2
$ref ||= "SCALAR"
l!l
5725
MC/DC100%1/1
$ref ||= "SCALAR"
$ref
Truth table100%2/2
A
A= $ref
Aresult
11
00
247  
248 82 my $get='_get_'.$ref;
249 82 my $return;
250 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
251 82 foreach my $v (reverse @vals) {
252 270 ▶ next unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 1 269 270
253 269 ▶ if (ref($v) eq "ARRAY" && $v->[0] eq $stop) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq 'ARRAY' and $v->[0] eq $Class::Data::TIN::stop) { } 17 252 269
Condition100%3/3
ref $v eq "ARRAY" and $v->[0] eq $Class::Data::TIN::stop
!ll&&!rl&&r
1737917
MC/DC100%2/2
ref $v eq "ARRAY" and $v->[0] eq $Class::Data::TIN::stop
ref $v eq "ARRAY" $v->[0] eq $Class::Data::TIN::stop
Truth table100%3/3
A and B
A= ref $v eq "ARRAY"B= $v->[0] eq $Class::Data::TIN::stop
ABresult
0X0
100
111
254 17 $return=$get->(undef,$v->[1]);
255 # my $overwrite=$v->[1];
256 # if ($ref eq "ARRAY") {
257 # $return=[];
258 # push(@$return,@$overwrite);
259 # } else {
260 # $return=$overwrite;
261 # }
262 } else {
263 252 $return=$get->($return,$v);
264 }
265 }
266 82 return $return;
267 }
268  
269  
270 72 ▶sub _get_SCALAR {
Subroutine _get_SCALAR: called CC=3 SCAR=11.0
271 72 my ($ret,$val)=@_;
272 72 my $r=ref($val);
273 72 ▶ if (!$r) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $r) { } 66 6 72
elsif ($r eq 'SCALAR') { } 6 0 6
274 66 $ret.=$val;
275 } elsif ($r eq "SCALAR") {
276 6 $ret=$$val;
277 }
278 72 return $ret;
279 }
280  
281 84 ▶sub _get_ARRAY {
Subroutine _get_ARRAY: called CC=1 SCAR=0.0
282 84 my ($ret,$val)=@_;
283 84 push(@$ret,@$val);
284 84 return $ret;
285 }
286  
287 104 ▶sub _get_HASH {
Subroutine _get_HASH: called CC=2 SCAR=6.9
288 104 my ($ret,$val)=@_;
289 104 ▶ if (! defined $ret) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ret) { } 36 68 104
290 36 $ret=$val;
291 } else {
292 68 $ret={%$ret,%$val};
293 }
294 104 return $ret;
295 }
296  
297 9 ▶sub _get_CODE {
Subroutine _get_CODE: called CC=1 SCAR=0.0
298 9 my ($ret,$val)=@_;
299 9 return $val;
300 }
301  
302 1 1;
303  
304 __END__
↑ Summary