Object/InsideOut/Foreign.pm

filter: Statementstmt 37.5% 45 / 120 Branchbran 22.7% 15 / 66 Conditioncond 25.0% 2 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 71.4% 5 / 7 Podpod n/a totaltotal 32.5% 67 / 206 CC 39 SCAR 50.4
CC
39
Cov
32%
__ANON__
342.0
__ANON__
72.0
__ANON__
17.3
CRAP 154.7
« Object/InsideOut/Autoload.pm ↑ Summary Object/InsideOut.pm »
1 package Object::InsideOut; {
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # Installs foreign inheritance methods
8 sub inherit
9 {
10 my ($GBL, $call, @args) = @_;
11 push(@{$$GBL{'export'}}, qw(inherit heritage disinherit));
12 $$GBL{'init'} = 1;
13  
14 *Object::InsideOut::inherit = sub
15 4 ▶ {
Subroutine __ANON__: called CC=12 SCAR=28.5
16 4 my $self = shift;
17  
18 # Must be called as an object method
19 4 my $obj_class = Scalar::Util::blessed($self);
20 4 ▶ if (! $obj_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj_class) 0 4 4
21 0 OIO::Method->die('message' => q/'inherit' called as a class method/);
22 }
23  
24 # Inheritance takes place in caller's package
25 4 my $pkg = caller();
26  
27 # Restrict usage to inside class hierarchy
28 4 ▶ if (! $obj_class->isa($pkg)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj_class->isa($pkg)) 0 4 4
29 0 OIO::Method->die('message' => "Can't call restricted method 'inherit' from class '$pkg'");
30 }
31  
32 # Flatten arg list
33 4 my (@arg_objs, $_arg);
34 4 ▶ while (defined($_arg = shift)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_arg = shift())) 4 4 8
35 4 ▶ if (ref($_arg) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_arg eq 'ARRAY') { } 0 4 4
36 0 push(@arg_objs, @{$_arg});
37 } else {
38 4 push(@arg_objs, $_arg);
39 }
40 }
41  
42 # Must be called with at least one arg
43 4 ▶ if (! @arg_objs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@arg_objs) 0 4 4
44 0 OIO::Args->die('message' => q/Missing arg(s) to '->inherit()'/);
45 }
46  
47 # Get 'heritage' field and 'classes' hash
48 4 my $herit = $$GBL{'heritage'};
49 4 ▶ if (! exists($$herit{$pkg})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $herit->{$pkg}) 0 4 4
50 0 create_heritage($pkg);
51 }
52 4 my $objects = $$herit{$pkg}{'obj'};
53 4 my $classes = $$herit{$pkg}{'cl'};
54  
55 # Process args
56 4 ▶ my $objs = exists($$objects{$$self}) ? $$objects{$$self} : [];
Branch: 1 branch
BranchTrueFalseTotal
exists $$objects{$$self} ? : 0 4 4
57 4 ▶ while (my $obj = shift(@arg_objs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = shift @arg_objs) 3 4 7
58 # Must be an object
59 3 my $arg_class = Scalar::Util::blessed($obj);
60 3 ▶ if (! $arg_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg_class) 0 3 3
61 0 OIO::Args->die('message' => q/Arg to '->inherit()' is not an object/);
62 }
63 # Must not be in class hierarchy
64 3 ▶ if ($obj_class->Object::InsideOut::SUPER::isa($arg_class) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($obj_class->Object::InsideOut::SUPER::isa($arg_class) or $arg_class->isa($obj_class)) 0 3 3
Condition33%1/3
$obj_class->Object::InsideOut::SUPER::isa($arg_class) or $arg_class->isa($obj_class)
l!l&&r!l&&!r
003
MC/DC0%0/2
$obj_class->Object::InsideOut::SUPER::isa($arg_class) or $arg_class->isa($obj_class)
$obj_class->Object::InsideOut::SUPER::isa($arg_class) $arg_class->isa($obj_class)
Truth table33%1/3
A or B
A= $obj_class->Object::InsideOut::SUPER::isa($arg_class)B= $arg_class->isa($obj_class)
ABresult
1X1
011
000
65 $arg_class->isa($obj_class))
66 {
67 0 OIO::Args->die('message' => q/Args to '->inherit()' cannot be within class hierarchy/);
68 }
69 # Add arg to object list
70 3 push(@{$objs}, $obj);
71 # Add arg class to classes hash
72 3 $$classes{$arg_class} = undef;
73 }
74 # Add objects to heritage field
75 4 $self->set($objects, $objs);
76 };
77  
78  
79 *Object::InsideOut::heritage = sub
80 0 ▶ {
Subroutine __ANON__: not called CC=8 SCAR=42.8
81 0 my $self = shift;
82  
83 # Must be called as an object method
84 0 my $obj_class = Scalar::Util::blessed($self);
85 0 ▶ if (! $obj_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj_class) 0 0 0
86 0 OIO::Method->die('message' => q/'heritage' called as a class method/);
87 }
88  
89 # Inheritance takes place in caller's package
90 0 my $pkg = caller();
91  
92 # Restrict usage to inside class hierarchy
93 0 ▶ if (! $obj_class->isa($pkg)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj_class->isa($pkg)) 0 0 0
94 0 OIO::Method->die('message' => "Can't call restricted method 'heritage' from class '$pkg'");
95 }
96  
97 # Anything to return?
98 0 ▶ if (! exists($$GBL{'heritage'}{$pkg}) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $GBL->{"heritage"}{$pkg} and exists $GBL->{"heritage"}{$pkg}{"obj"}{$$self}) 0 0 0
Condition0%0/3
exists $GBL->{"heritage"}{$pkg} and exists $GBL->{"heritage"}{$pkg}{"obj"}{$$self}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $GBL->{"heritage"}{$pkg} and exists $GBL->{"heritage"}{$pkg}{"obj"}{$$self}
exists $GBL->{"heritage"}{$pkg} exists $GBL->{"heritage"}{$pkg}{"obj"}{$$self}
Truth table0%0/3
A and B
A= exists $GBL->{"heritage"}{$pkg}B= exists $GBL->{"heritage"}{$pkg}{"obj"}{$$self}
ABresult
0X0
100
111
99 ! exists($$GBL{'heritage'}{$pkg}{'obj'}{$$self}))
100 {
101 0 return;
102 }
103  
104 0 my @objs;
105 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
106 # Filter by specified classes
107 @objs = grep {
108 0 my $obj = $_;
109 0 grep { ref($obj) eq $_ } @_
110 0 } @{$$GBL{'heritage'}{$pkg}{'obj'}{$$self}};
111 } else {
112 # Return entire list
113 0 @objs = @{$$GBL{'heritage'}{$pkg}{'obj'}{$$self}};
114 }
115  
116 # Return results
117 0 ▶ if (wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
118 0 return (@objs);
119 }
120 0 ▶ if (@objs == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@objs == 1) 0 0 0
121 0 return ($objs[0]);
122 }
123 0 return (\@objs);
124 };
125  
126  
127 *Object::InsideOut::disinherit = sub
128 0 ▶ {
Subroutine __ANON__: not called CC=18 SCAR=58.3
129 0 my $self = shift;
130  
131 # Must be called as an object method
132 0 my $class = Scalar::Util::blessed($self);
133 0 ▶ if (! $class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 0 0 0
134 0 OIO::Method->die('message' => q/'disinherit' called as a class method/);
135 }
136  
137 # Disinheritance takes place in caller's package
138 0 my $pkg = caller();
139  
140 # Restrict usage to inside class hierarchy
141 0 ▶ if (! $class->isa($pkg)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->isa($pkg)) 0 0 0
142 0 OIO::Method->die('message' => "Can't call restricted method 'disinherit' from class '$pkg'");
143 }
144  
145 # Flatten arg list
146 0 my (@args, $_arg);
147 0 ▶ while (defined($_arg = shift)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_arg = shift())) 0 0 0
148 0 ▶ if (ref($_arg) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_arg eq 'ARRAY') { } 0 0 0
149 0 push(@args, @{$_arg});
150 } else {
151 0 push(@args, $_arg);
152 }
153 }
154  
155 # Must be called with at least one arg
156 0 ▶ if (! @args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@args) 0 0 0
157 0 OIO::Args->die('message' => q/Missing arg(s) to '->disinherit()'/);
158 }
159  
160 # Get 'heritage' field
161 0 ▶ if (! exists($$GBL{'heritage'}{$pkg})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $GBL->{"heritage"}{$pkg}) 0 0 0
162 0 OIO::Code->die(
163 'message' => 'Nothing to ->disinherit()',
164 'Info' => "Class '$pkg' is currently not inheriting from any foreign classes");
165 }
166 0 my $objects = $$GBL{'heritage'}{$pkg}{'obj'};
167  
168 # Get inherited objects
169 0 ▶ my @objs = exists($$objects{$$self}) ? @{$$objects{$$self}} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $$objects{$$self} ? : 0 0 0
170  
171 # Check that object is inheriting all args
172 0 foreach my $arg (@args) {
173 0 ▶ if (Scalar::Util::blessed($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($arg)) { } 0 0 0
174 # Arg is an object
175 0 ▶ if (! grep { $_ == $arg } @objs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ == $arg;} @objs) 0 0 0
176 0 my $arg_class = ref($arg);
177 0 OIO::Args->die(
178 'message' => 'Cannot ->disinherit()',
179 'Info' => "Object is not inheriting from an object of class '$arg_class' inside class '$class'");
180 }
181 } else {
182 # Arg is a class
183 0 ▶ if (! grep { ref($_) eq $arg } @objs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {ref $_ eq $arg;} @objs) 0 0 0
184 0 OIO::Args->die(
185 'message' => 'Cannot ->disinherit()',
186 'Info' => "Object is not inheriting from an object of class '$arg' inside class '$class'");
187 }
188 }
189 }
190  
191 # Delete args from object
192 0 my @new_list = ();
193 OBJECT:
194 0 foreach my $obj (@objs) {
195 0 foreach my $arg (@args) {
196 0 ▶ if (Scalar::Util::blessed($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($arg)) { } 0 0 0
197 0 ▶ if ($obj == $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj == $arg) 0 0 0
198 0 next OBJECT;
199 }
200 } else {
201 0 ▶ if (ref($obj) eq $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj eq $arg) 0 0 0
202 0 next OBJECT;
203 }
204 }
205 }
206 0 push(@new_list, $obj);
207 }
208  
209 # Set new object list
210 0 ▶ if (@new_list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@new_list) { } 0 0 0
211 0 $self->set($objects, \@new_list);
212 } else {
213 # No objects left
214 0 delete($$objects{$$self});
215 }
216 };
217  
218  
219 *Object::InsideOut::create_heritage = sub
220 6 ▶ {
Subroutine __ANON__: called CC=4 SCAR=14.9
221 # Private
222 6 my $caller = caller();
223 6 ▶ if ($caller ne 'Object::InsideOut') {
Branch: 1 branch
BranchTrueFalseTotal
if ($caller ne "Object::InsideOut") 0 6 6
224 0 OIO::Method->die('message' => "Can't call private subroutine 'Object::InsideOut::create_heritage' from class '$caller'");
225 }
226  
227 6 my $pkg = shift;
228  
229 # Check if 'heritage' already exists
230 6 ▶ if (exists($$GBL{'dump'}{'fld'}{$pkg}{'heritage'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $GBL->{"dump"}{"fld"}{$pkg}{"heritage"}) 0 6 6
231 0 OIO::Attribute->die(
232 'message' => "Can't inherit into '$pkg'",
233 'Info' => "'heritage' already specified for another field using '$$GBL{'dump'}{'fld'}{$pkg}{'heritage'}{'src'}'");
234 }
235  
236 # Create the heritage field
237 6 my $objects = {};
238  
239 # Share the field, if applicable
240 6 ▶ if (is_sharing($pkg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_sharing($pkg)) 0 6 6
241 0 threads::shared::share($objects)
242 }
243  
244 # Save the field's ref
245 6 push(@{$$GBL{'fld'}{'ref'}{$pkg}}, $objects);
246  
247 # Save info for ->dump()
248 6 $$GBL{'dump'}{'fld'}{$pkg}{'heritage'} = {
249 fld => $objects,
250 src => 'Inherit'
251 };
252  
253 # Save heritage info
254 6 $$GBL{'heritage'}{$pkg} = {
255 obj => $objects,
256 cl => {}
257 };
258  
259 # Set up UNIVERSAL::can/isa to handle foreign inheritance
260 6 install_UNIVERSAL();
261 };
262  
263  
264 # Do the original call
265 @_ = @args;
266 goto &$call;
267 }
268  
269 } # End of package's lexical scope
270  
271  
272 # Ensure correct versioning
273 3 ▶($Object::InsideOut::VERSION eq '4.05')
Condition50%1/2
$VERSION eq '4.05' || die("Version mismatch\n")
l!l
30
MC/DC0%0/1
$VERSION eq '4.05' || die("Version mismatch\n")
$VERSION eq '4.05'
Truth table50%1/2
A
A= $VERSION eq '4.05'
Aresult
11
00
274 or die("Version mismatch\n");
275  
276 # EOF
« Object/InsideOut/Autoload.pm ↑ Summary Object/InsideOut.pm »