BaseClass.pm

filter: Statementstmt 25.8% 92 / 356 Branchbran 6.1% 10 / 162 Conditioncond 15.4% 11 / 71 MC/DCmcdc 4.7% 2 / 42 Subroutinesub 41.3% 12 / 29 Podpod 80.0% 4 / 5 totaltotal 19.7% 131 / 665 CC 135 SCAR 73.8
CC
135
Cov
19%
check4Clashes
3050.9
AUTOLOAD
182.0
clone
132.0
CRAP 1608.5
↑ Summary Interface.pm »
1 package MRP::BaseClass;
2  
3 1 ▶use Exporter;
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 MRP::Introspection;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MRP::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use MRP::Interface;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use vars qw($AUTOLOAD %cache $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 $VERSION = 1.0;
14  
15 # print nice diagnostics when functions are incorrectly called
16 #
17 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=13 SCAR=52.0
18 0 my ($package,$name) = $AUTOLOAD =~ /^(.+?)::([^:]+)$/;
19 0 ▶ return if $name eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "DESTROY" 0 0 0
20 #print "Called with @_ and autoload $AUTOLOAD\n";
21 0 ▶ my $self = shift || die $package."->".$name." called for nothing!";
Condition0%0/2
shift() || die($package . '->' . $name . ' called for nothing!')
l!l
00
MC/DC0%0/1
shift() || die($package . '->' . $name . ' called for nothing!')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
22 0 ▶ my $ref = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
23 0 ▶ my $message = ($ref eq $self) ? "Can't access '$name' in class $ref:\n" :
Branch: 1 branch
BranchTrueFalseTotal
$ref eq $self ? : 0 0 0
24 "Can't access '$name' in object $self:\n";
25 0 my ($member,$func);
26 0 ▶ if($^W) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^W) 0 0 0
27 0 ▶ if(ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 0 0 0
28 0 foreach $member (sort keys %$self) {
29 0 ▶ if( $member =~ /$name/i || $name =~ /$member/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($member =~ /$name/i or $name =~ /$member/i) 0 0 0
Condition0%0/3
$member =~ /$name/i or $name =~ /$member/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$member =~ /$name/i or $name =~ /$member/i
$member =~ /$name/i $name =~ /$member/i
Truth table0%0/3
A or B
A= $member =~ /$name/iB= $name =~ /$member/i
ABresult
1X1
011
000
30 0 $message .= "Did you mean member\t'$member'?\n";
31 }
32 }
33 }
34 0 my %funcs = MRP::Introspection::recursiveInheritance($self,'MRP::Introspection::functions');
35 0 foreach $func (sort keys %funcs) {
36 0 my ($funcn) = $func =~ /([^:]+)$/;
37 0 ▶ if( $funcn =~ /$name/i || $name =~ /$funcn/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($funcn =~ /$name/i or $name =~ /$funcn/i) 0 0 0
Condition0%0/3
$funcn =~ /$name/i or $name =~ /$funcn/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$funcn =~ /$name/i or $name =~ /$funcn/i
$funcn =~ /$name/i $name =~ /$funcn/i
Truth table0%0/3
A or B
A= $funcn =~ /$name/iB= $name =~ /$funcn/i
ABresult
1X1
011
000
38 0 $message .= "Did you mean function\t'$func'?\n";
39 }
40 }
41 0 my ($package, $filename, $line) = caller;
42 0 $message .= "in $filename at line $line\n";
43 0 $message .= "\n".$self->_printMembers()."\n";
44 }
45 0 confess $message;
46 }
47  
48 # returns a MRP::MyBase object
49 # use this for all derived classes
50 #
51 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
52 0 my $ref = shift;
53 0 ▶ my $class = ref($ref) || $ref;
Condition0%0/3
ref $ref || $ref
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $ref || $ref
ref $ref $ref
Truth table0%0/3
A || B
A= ref $refB= $ref
ABresult
1X1
011
000
54 0 my $self = {};
55
56 0 rebless($self,$class);
57  
58 # print "Created $self\n";
59 0 return $self;
60 }
61  
62 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
63 # my $self = shift;
64 # print "Destroying $self\n";
65 }
66  
67 0 ▶sub clone {
Subroutine clone: not called CC=11 SCAR=48.8
Pod documented
68 0 my $self = shift;
69 0 my $depth = shift;
70 0 my $clone = {};
71 0 my ($name, $value);
72 0 my $ref;
73  
74 0 ▶ $depth = defined($depth)
Branch: 2 branches
BranchTrueFalseTotal
defined $depth ? : 0 0 0
$depth =~ /'shallow'/ ? : 0 0 0
75 ? ($depth =~ /'shallow'/) ? '' : 1
76 : '';
77  
78 0 ▶ while (($name, $value) = each %$self) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$self) 0 0 0
79 0 ▶ if($depth) { # if we are doing a deep copy
Branch: 1 branch
BranchTrueFalseTotal
if ($depth) { } 0 0 0
80 0 ▶ if($ref = ref($value)) { # and we have a reference
Branch: 1 branch
BranchTrueFalseTotal
if ($ref = ref $value) { } 0 0 0
81 0 for($ref) { # what type of reference?
82 0 ▶ /^HASH$/ && do { %{$self->{$name}} = %$value; next; };
Branch: 1 branch
BranchTrueFalseTotal
/^HASH$/ and do { %{$$self{$name};} = %$value; next } 0 0 0
83 0 ▶ /^ARRAY$/ && do { @{$self->{$name}} = @$value; next; };
Branch: 1 branch
BranchTrueFalseTotal
/^ARRAY$/ and do { @{$$self{$name};} = @$value; next } 0 0 0
84 0 ▶ /^SCALAR$/ && do { my $tmp = $$value; $self->{$name} = \$tmp; next; };
Branch: 1 branch
BranchTrueFalseTotal
/^SCALAR$/ and do { my $tmp = $$value; $self->{$name} = \$tmp; next } 0 0 0
85 0 ▶ $clone->{$name} = $value->can('clone') # and clone this object
Branch: 1 branch
BranchTrueFalseTotal
$value->can('clone') ? : 0 0 0
86 ? $value->clone() # by hook
87 : $value->MRP::BaseClass::clone($depth); # or by crook
88 }
89 } else {
90 0 $clone->{$name} = $value;
91 }
92 } else { # if wee are doing a shallow copy
93 0 $clone->{$name} = $value; # add each filed itself.
94 }
95 }
96
97 0 bless $clone, ref $self;
98 0 return $clone;
99 }
100  
101 # reblesses a reference to a base class object into your class
102 # adds the apropreate members with their default values as
103 # defined in the %fields hash, and modified by initialize
104 # If the default value for any of the fields is a hash or
105 # array ref, puts in a new ref for the object.
106 #
107  
108 0 ▶sub rebless {
Subroutine rebless: not called CC=10 SCAR=47.0
Pod documented
109 0 my ($self,$class,%fields);
110 0 ▶ if(ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
111 0 ▶ $self = shift || confess "You must give an object to rebless";
Condition0%0/2
shift() || confess('You must give an object to rebless')
l!l
00
MC/DC0%0/1
shift() || confess('You must give an object to rebless')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
112 0 ▶ $class = shift || confess "You must give a package to rebless $self into";
Condition0%0/2
shift() || confess("You must give a package to rebless $self into")
l!l
00
MC/DC0%0/1
shift() || confess("You must give a package to rebless $self into")
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
113 } else {
114 0 $class = shift;
115 0 $self = {};
116 0 my @parents = @_;
117 0 ▶ %$self = map { ref $_
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
118 ? %$_
119 : confess "You tried to treat scalar $_ as an object for rebless"
120 } @parents; # add all the parental fileds to this object
121 #print "Multiple inheritance. $self is created from ", MRP::Text->pretyArray(@parents), "\n";
122 }
123  
124 # add this classes fields to the object
125 0 %fields = _fields($class);
126 0 foreach my $field (sort keys %fields) {
127 0 my $val = $fields{$field};
128 0 for (ref $val) {
129 0 ▶ /^ARRAY$/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^ARRAY$/ and do { $fields{$field} = []; last } 0 0 0
130 0 $fields{$field} = [];
131 0 last;
132 };
133 0 ▶ /^HASH$/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^HASH$/ and do { $fields{$field} = {}; last } 0 0 0
134 0 $fields{$field} = {};
135 0 last;
136 };
137 }
138 }
139 0 %{$self} = (%{$self}, %fields);
140 #print "Reblessing $self into $class\n";
141 0 bless $self, $class;
142 0 my $initialize = MRP::Introspection::function($self,'initialize');
143 0 ▶ &$initialize($self) if defined $initialize;
Branch: 1 branch
BranchTrueFalseTotal
if defined $initialize 0 0 0
144  
145 0 return $self;
146 }
147  
148 # prints a list of all members out - nice but not necisary. Will be lost
149 #
150 0 ▶sub _printMembers {
Subroutine _printMembers: not called CC=9 SCAR=45.0
151 0 my $self = shift;
152 0 my ($key, $val);
153 0 my ($func, $scalar, $hash, $array, $ref);
154 0 my $text;
155 0 local $^W = undef;
156 0 ▶ if(ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 0 0 0
157 0 $text = "Dumping model for $self\n";
158 0 $text .= "Members\n";
159 0 $text .= MRP::Text->pretyHash("\t", $self);
160 }
161 0 $text .= "Methods\n";
162 0 my %funcs = MRP::Introspection::recursiveInheritance($self,'MRP::Introspection::functions');
163 0 foreach $func (sort keys %funcs) {
164 0 $ref = $funcs{$func};
165 0 ▶ $func =~ /:_/ && next;
Branch: 1 branch
BranchTrueFalseTotal
$func =~ /:_/ and next 0 0 0
166 0 ▶ $func =~ /:memberAccess::.*?_/ && next;
Branch: 1 branch
BranchTrueFalseTotal
$func =~ /:memberAccess::.*?_/ and next 0 0 0
167 0 ▶ $func =~ /(croak)|(confess)|(carp)/ && next;
Branch: 1 branch
BranchTrueFalseTotal
$func =~ /(croak)|(confess)|(carp)/ and next 0 0 0
168 0 $text .= "\t$func\t= $ref\n";
169 }
170 0 $text .= "Variables\n";
171 0 my %scalars = MRP::Introspection::scalars($self);
172 0 ▶ while (($scalar, $ref) = each %scalars) {
Branch: 1 branch
BranchTrueFalseTotal
if (($scalar, $ref) = each %scalars) 0 0 0
173 0 $text .= "\t\$$scalar\t= $$ref\n";
174 }
175 0 my %hashes = MRP::Introspection::hashes($self);
176 0 ▶ while (($hash, $ref) = each %hashes) {
Branch: 1 branch
BranchTrueFalseTotal
if (($hash, $ref) = each %hashes) 0 0 0
177 0 $text .= "\t\%$hash\t= $ref=\n".MRP::Text->pretyHash("\t\t", $ref)."\n";
178 }
179 0 my %arrays = MRP::Introspection::arrays($self);
180 0 ▶ while (($array, $ref) = each %arrays) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %arrays) 0 0 0
181 0 $text .= "\t\@$array\t= $ref=".MRP::Text->pretyArray($ref)."\n";
182 }
183 0 return $text;
184 }
185  
186 4 ▶sub _fields ($) {
Subroutine _fields: called CC=3 SCAR=14.2
187 4 my $thingy = shift;
188 4 my $fields = MRP::Introspection::hash($thingy,'fields');
189 4 ▶ return () unless defined $fields;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fields 4 0 4
190 return (wantarray)
191 0 ▶ ? %$fields
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
192 : $fields;
193 }
194  
195 # checks for name clashes between a class and all of it's base classes and delegates.
196 # Supports multiple inheritance, and multi-level inheritance
197 #
198 # also makes sure that $package::memberAccess is at the beginning of @$package::INC
199 2 ▶sub check4Clashes {
Subroutine check4Clashes: called CC=84 SCAR=80.2
Pod documented
200 2 my $class = shift;
201 2 my %debug = @_;
202 2 my $memberPackage = $class."::memberAccess";
203 # print "Checking $class\n";
204 # open the debug streams
205 2 my ($VARIABLES, $DELEGATES);
206 2 ▶ if(exists $debug{'variables'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $debug{'variables'}) { } 0 2 2
207 0 ▶ unless(defined(my $fh = $debug{'variables'})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $fh = $debug{'variables'})) { } 0 0 0
if (do { ref $fh eq 'GLOB' }) { } 0 0 0
208 0 $VARIABLES = \*STDERR;
209 } elsif (ref($fh) eq 'GLOB') {
210 0 $VARIABLES = $fh;
211 0 $debug{'variables'} = undef;
212 } else {
213 0 ▶ open(VARIABLES, ">>$fh") || die "Could not open $fh for appending: $!";
Branch: 1 branch
BranchTrueFalseTotal
open VARIABLES, ">>$fh" or die "Could not open $fh for appending: $!" 0 0 0
214 0 $VARIABLES = \*VARIABLES;
215 0 $debug{'variables'} = 'close';
216 }
217 2 } else { $VARIABLES = undef; }
218 2 ▶ if(exists $debug{'delegates'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $debug{'delegates'}) { } 0 2 2
219 0 ▶ unless(defined(my $fh = $debug{'delegates'})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $fh = $debug{'delegates'})) { } 0 0 0
if (do { ref $fh eq 'GLOB' }) { } 0 0 0
220 0 $DELEGATES = \*STDERR;
221 } elsif (ref($fh) eq 'GLOB') {
222 0 $DELEGATES = $fh;
223 0 $debug{'delegates'} = undef;
224 } else {
225 0 ▶ open(DELEGATES, ">>$fh") || die "Could not open $fh for output: $!";
Branch: 1 branch
BranchTrueFalseTotal
open DELEGATES, ">>$fh" or die "Could not open $fh for output: $!" 0 0 0
226 0 $DELEGATES = \*DELEGATES;
227 0 $debug{'delegates'} = 'close';
228 }
229 2 } else { $DELEGATES = undef; }
230
231 # get the fields has for $class
232 2 ▶ my $fields = $class->MRP::BaseClass::_fields() || {};
Condition50%1/2
$class->MRP::BaseClass::_fields || {}
l!l
02
MC/DC0%0/1
$class->MRP::BaseClass::_fields || {}
$class->MRP::BaseClass::_fields
Truth table50%1/2
A
A= $class->MRP::BaseClass::_fields
Aresult
11
00
233 2 ▶ my $delegates = MRP::Introspection::hash($class,'delegates') || {};
Condition50%1/2
&MRP::Introspection::hash($class, 'delegates') || {}
l!l
02
MC/DC0%0/1
&MRP::Introspection::hash($class, 'delegates') || {}
&MRP::Introspection::hash($class, 'delegates')
Truth table50%1/2
A
A= &MRP::Introspection::hash($class, 'delegates')
Aresult
11
00
234 2 ▶ my $variables = MRP::Introspection::array($class,'public_vars') || [];
Condition50%1/2
&MRP::Introspection::array($class, 'public_vars') || []
l!l
02
MC/DC0%0/1
&MRP::Introspection::array($class, 'public_vars') || []
&MRP::Introspection::array($class, 'public_vars')
Truth table50%1/2
A
A= &MRP::Introspection::array($class, 'public_vars')
Aresult
11
00
235 my %variables = map {
236 0 /^([\@\$\%])(.*)$/;
237 0 ▶ ($2,($1 eq '@')
Branch: 2 branches
BranchTrueFalseTotal
$1 eq '@' ? : 0 0 0
$1 eq '%' ? : 0 0 0
238 ? []
239 : ($1 eq '%')
240 ? {} : undef
241 )
242 } @$variables;
243 2 ▶ my $default = MRP::Introspection::array($class,'defaults') || [];
Condition50%1/2
&MRP::Introspection::array($class, 'defaults') || []
l!l
02
MC/DC0%0/1
&MRP::Introspection::array($class, 'defaults') || []
&MRP::Introspection::array($class, 'defaults')
Truth table50%1/2
A
A= &MRP::Introspection::array($class, 'defaults')
Aresult
11
00
244 my %default = map {
245 0 /^([\@\$\%])(.*)$/;
246 0 ▶ ($2,($1 eq '@')
Branch: 2 branches
BranchTrueFalseTotal
$1 eq '@' ? : 0 0 0
$1 eq '%' ? : 0 0 0
247 ? []
248 : ($1 eq '%')
249 ? {} : undef
250 )
251 } @$default;
252 0 my %all = map { ($_,1) } keys %$fields, keys %variables, keys %default;
253 2 ▶ my $isaref = MRP::Introspection::ISA($class) || [];
Condition50%1/2
&MRP::Introspection::ISA($class) || []
l!l
20
MC/DC0%0/1
&MRP::Introspection::ISA($class) || []
&MRP::Introspection::ISA($class)
Truth table50%1/2
A
A= &MRP::Introspection::ISA($class)
Aresult
11
00
254 2 my $override;
255 2 my @found = ();
256
257 # check for ambiguous functions
258 2 my $isaCache;
259 2 my @lISA = @$isaref;
260  
261 2 ▶ if(@lISA) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lISA) 1 1 2
262 1 my $firstParent = shift @lISA;
263 1 $isaCache = $cache{$firstParent};
264 1 ▶ unless($isaCache) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($isaCache) 1 0 1
265 1 $firstParent->MRP::BaseClass::check4Clashes();
266 1 $isaCache = $cache{$firstParent};
267 }
268 # print "$class: Adding $firstParent\n";
269 }
270  
271 2 my @clashes;
272 2 ▶ my %allfunctions = %{scalar($isaCache->{'functions'}) || {}};
Condition100%2/2
scalar $isaCache->{'functions'} || {}
l!l
11
MC/DC100%1/1
scalar $isaCache->{'functions'} || {}
scalar $isaCache->{'functions'}
Truth table100%2/2
A
A= scalar $isaCache->{'functions'}
Aresult
11
00
273 2 ▶ my %allfields = %{scalar($isaCache->{'fields'}) || {}};
Condition100%2/2
scalar $isaCache->{'fields'} || {}
l!l
11
MC/DC100%1/1
scalar $isaCache->{'fields'} || {}
scalar $isaCache->{'fields'}
Truth table100%2/2
A
A= scalar $isaCache->{'fields'}
Aresult
11
00
274 2 my $classCache = {functions=>{},fields=>{}};
275 2 my %classFunctions = MRP::Introspection::functions($class);
276 2 my %classFields = MRP::BaseClass->_fields($class);
277 2 foreach (keys %classFunctions) { $classFunctions{$_} = [$class, $classFunctions{$_}] }
278 0 foreach (keys %classFields) { $classFields{$_} = $class }
279  
280 2 foreach my $isa (@lISA) {
281 # print "$class: Checking $isa for clashes\n";
282 0 ▶ my $isaCache = $cache{$isa} || do {$isa->MRP::BaseClass::check4Clashes, $cache{$isa}};
Condition0%0/3
$MRP::BaseClass::cache{$isa} || do { $isa->MRP::BaseClass::check4Clashes, $MRP::BaseClass::cache{$isa} }
l!l&&r!l&&!r
000
MC/DC0%0/2
$MRP::BaseClass::cache{$isa} || do { $isa->MRP::BaseClass::check4Clashes, $MRP::BaseClass::cache{$isa} }
$MRP::BaseClass::cache{$isa} do { $isa->MRP::BaseClass::check4Clashes, $MRP::BaseClass::cache{$isa} }
Truth table0%0/3
A || B
A= $MRP::BaseClass::cache{$isa}B= do { $isa->MRP::BaseClass::check4Clashes, $MRP::BaseClass::cache{$isa} }
ABresult
1X1
011
000
283 0 ▶ my %functions = %{scalar($isaCache->{'functions'}) || {}};
Condition0%0/2
scalar $isaCache->{'functions'} || {}
l!l
00
MC/DC0%0/1
scalar $isaCache->{'functions'} || {}
scalar $isaCache->{'functions'}
Truth table0%0/2
A
A= scalar $isaCache->{'functions'}
Aresult
11
00
284 0 ▶ my %fields = %{scalar($isaCache->{'fields'}) || {}};
Condition0%0/2
scalar $isaCache->{'fields'} || {}
l!l
00
MC/DC0%0/1
scalar $isaCache->{'fields'} || {}
scalar $isaCache->{'fields'}
Truth table0%0/2
A
A= scalar $isaCache->{'fields'}
Aresult
11
00
285 0 foreach my $function (keys %functions) {
286 0 my ($package,$ref) = @{$functions{$function}};
287 0 ▶ if(my $clash = $allfunctions{$function}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $clash = $allfunctions{$function}) 0 0 0
288 0 ▶ if($clash->[1] ne $ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clash->[1] ne $ref) 0 0 0
289 0 ▶ unless($classFunctions{$function}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($classFunctions{$function}) 0 0 0
290 0 push @clashes, "$package->$function\t <----->\t$clash->[0]"."->$function";
291 }
292 }
293 }
294 0 $allfunctions{$function} = $functions{$function};
295 }
296 0 foreach my $field (keys %fields) {
297 0 ▶ if(my $clash = $allfields{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $clash = $allfields{$field}) 0 0 0
298 0 push @clashes, "method: $isa->$field\t <----->\t$clash->$field";
299 }
300 0 $allfields{$field} = $fields{$field};
301 }
302 }
303  
304 2 foreach my $delegate (keys %$delegates) {
305 0 my $list = $delegates->{$delegate};
306 0 ▶ ref $list || next;
Branch: 1 branch
BranchTrueFalseTotal
ref $list or next 0 0 0
307 0 my @list;
308 0 foreach my $func (@$list) {
309 0 ▶ (ref($func)) and
Branch: 1 branch
BranchTrueFalseTotal
ref $func and push @list, $func->functions or push @list, $func 0 0 0
Condition0%0/3
ref $func and push @list, $func->functions
!ll&&!rl&&r
000
MC/DC0%0/2
ref $func and push @list, $func->functions
ref $func push @list, $func->functions
Truth table0%0/3
A and B
A= ref $funcB= push @list, $func->functions
ABresult
0X0
100
111
310 push @list, $func->functions() or
311 push @list, $func;
312 }
313 0 foreach my $func (@list) {
314 0 ▶ if(my $clash = $allfunctions{$func}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $clash = $allfunctions{$func}) 0 0 0
315 0 ▶ unless($classFunctions{$func}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($classFunctions{$func}) 0 0 0
316 0 push @clashes, "delegate: $delegate->$func\t <-----> " . $clash->[0] . "->$func";
317 }
318 }
319 }
320 }
321  
322 2 %allfunctions = (%allfunctions, %classFunctions);
323 2 %allfields = (%allfields, %classFields);
324  
325 # check that delegate functions do not clash with fields or inherited functions
326 2 foreach my $field (keys %$fields) {
327 0 foreach my $delegate (keys %$delegates) {
328 push @clashes,
329 0 ▶ map { ($field eq $_ and
Branch: 1 branch
BranchTrueFalseTotal
$field eq $_ && !&MRP::Introspection::function($class, $field) ? : 0 0 0
Condition0%0/3
$field eq $_ and not &MRP::Introspection::function($class, $field)
!ll&&!rl&&r
000
MC/DC0%0/2
$field eq $_ and not &MRP::Introspection::function($class, $field)
$field eq $_ not &MRP::Introspection::function($class, $field)
Truth table0%0/3
A and not(B)
A= $field eq $_B= not &MRP::Introspection::function($class, $field)
ABresult
0X0
100
111
330 not MRP::Introspection::function($class,$field))
331 ? "field $field clashes with $delegate->$_"
332 : ();
333 0 } @{$delegates->{$delegate}}
334 }
335 }
336  
337 # check that default variables don't clash with package/field or inherited field variables
338 2 foreach my $default (keys %default) {
339 0 ▶ if(exists $fields->{$default}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $fields->{$default}) { } 0 0 0
elsif (exists $allfields{$default}) { } 0 0 0
elsif (exists $variables{$default}) { } 0 0 0
340 0 push @clashes, "default $default clashes with field";
341 } elsif (exists $allfields{$default}) {
342 0 push @clashes, "default $default clashes with inherited field";
343 } elsif (exists $variables{$default}) {
344 0 push @clashes, "default $default clashes with variable";
345 }
346 }
347 0 %$fields = (%$fields, map { ($_,undef) } keys %default);
348  
349 2 ▶ if(@clashes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@clashes) 0 2 2
350 0 die "The following parts of $class are ambiguous:\n",
351 join "\n",@clashes,"\n";
352 }
353  
354 2 my $func;
355 # add the member access functions
356 2 foreach my $item (keys %all) {
357 0 my $item_ref = $item.'_ref';
358 0 my $item_field = $item.'_field';
359 0 my $item_global = $item.'_global';
360 0 ▶ MRP::Introspection::function($memberPackage,$item)
Branch: 1 branch
BranchTrueFalseTotal
&MRP::Introspection::function($memberPackage, $item) and confess("$item already exists in $memberPackage\n") 0 0 0
361 && confess "$item already exists in $memberPackage\n";
362 # generate the @default member access functions
363 0 ▶ if(exists $default{$item}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $default{$item}) { } 0 0 0
elsif (exists $fields->{$item}) { } 0 0 0
364 #print "Generating defualt function for '$item'\n";
365 0 for (ref $default{$item}) {
366 0 ▶ if(/^ARRAY$/) { # this is an array member
Branch: 2 branches
BranchTrueFalseTotal
if (/^ARRAY$/) { } 0 0 0
elsif (/^HASH$/) { } 0 0 0
367 0 $func = $class->_fieldArrayFunc($memberPackage,$item_field,$item);
368 0 $func.= $class->_packageArrayFunc($memberPackage,$item_global,$item);
369 } elsif (/^HASH$/) { # this is a hash member
370 0 $func = $class->_fieldHashFunc($memberPackage,$item_field,$item);
371 0 $func.= $class->_packageHashFunc($memberPackage,$item_global,$item);
372 } else {
373 0 $func = $class->_fieldScalarFunc($memberPackage,$item_field,$item,$fields->{$item});
374 0 $func.= $class->_packageScalarFunc($memberPackage,$item_global,$item);
375 }
376 }
377 0 $func .= $class->_defaultField($memberPackage, $item,
378 $item_field, $item_global)
379 } elsif(exists $fields->{$item}) {
380 0 ▶ if(exists $variables{$item}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $variables{$item}) { } 0 0 0
381 # Generate access functions for duel field/package items
382 #print "generating dual acces for '$item'\n";
383 0 for (ref $fields->{$item}) {
384 0 ▶ if(/^ARRAY$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^ARRAY$/) { } 0 0 0
elsif (/^HASH$/) { } 0 0 0
385 0 $func = $class->_fieldArrayFunc($memberPackage,$item_field,$item);
386 } elsif (/^HASH$/) {
387 0 $func = $class->_fieldHashFunc($memberPackage,$item_field,$item);
388 } else {
389 0 $func = $class->_fieldScalarFunc($memberPackage,$item_field,$item,$fields->{$item});
390 }
391 }
392 0 for (ref $variables{$item}) {
393 0 ▶ if(/^ARRAY$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^ARRAY$/) { } 0 0 0
elsif (/^HASH$/) { } 0 0 0
394 0 $func .= $class->_packageArrayFunc($memberPackage,$item_global,$item);
395 } elsif (/^HASH$/) {
396 0 $func .= $class->_packageHashFunc($memberPackage,$item_global,$item);
397 } else {
398 0 $func .= $class->_packageScalarFunc($memberPackage,$item_global,$item);
399 }
400 }
401 0 $func .= $class->_packageAndField($memberPackage, $item,
402 $item_field, $item_global)
403 } else {
404 # generate field member functions
405 #print "generating field '$item'\n";
406 0 for (ref $fields->{$item}) {
407 0 ▶ if(/^ARRAY$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^ARRAY$/) { } 0 0 0
elsif (/^HASH$/) { } 0 0 0
408 0 $func = $class->_fieldArrayFunc($memberPackage,$item);
409 } elsif (/^HASH$/) {
410 0 $func = $class->_fieldHashFunc($memberPackage,$item);
411 } else {
412 0 $func = $class->_fieldScalarFunc($memberPackage,$item,$fields->{$item});
413 }
414 }
415 }
416 } else {
417 # generate package member functions
418 #print "Generating package member function '$item'";
419 0 for (ref $variables{$item}) {
420 0 ▶ if(/^ARRAY$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^ARRAY$/) { } 0 0 0
elsif (/^HASH$/) { } 0 0 0
421 0 $func = $class->_packageArrayFunc($memberPackage,$item);
422 } elsif (/^HASH$/) {
423 0 $func = $class->_packageHashFunc($memberPackage,$item);
424 } else {
425 0 $func = $class->_packageScalarFunc($memberPackage,$item);
426 }
427 }
428 }
429 0 ▶ $VARIABLES && print $VARIABLES $func;
Branch: 1 branch
BranchTrueFalseTotal
$VARIABLES and print $VARIABLES $func 0 0 0
430 # print "Compiling member access:\n$func\n";
431 0 ▶ eval $func; $@ && die "Error compiling code: $@";
Branch: 1 branch
BranchTrueFalseTotal
$@ and die "Error compiling code: $@" 0 0 0
432 }
433 # add the delegation functions
434 2 foreach my $delegate (keys %$delegates) {
435 # print "Processing delegate $delegate\n";
436 0 ▶ exists $fields->{$delegate} and
Branch: 1 branch
BranchTrueFalseTotal
exists $fields->{$delegate} and die "You have specified $class->$delegate as a delegate but there is a field by that name" 0 0 0
437 die "You have specified $class->$delegate as a delegate but there is a field by that name";
438 0 my @interfaces = ();
439 0 my $func = "";
440 0 foreach my $item (@{$delegates->{$delegate}}) {
441 # print "Generating delegate for '$delegate->$item'\n";
442 0 ▶ if(ref($item)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item) { } 0 0 0
443 0 $func .= join '', map { $class->_delegateFunc($memberPackage,$delegate,$_) } ($item->functions());
444 0 push @interfaces, $item;
445 } else {
446 0 $func .= $class->_delegateFunc($memberPackage,$delegate,$item); # make a function for it
447 }
448 0 ▶ $DELEGATES && print $DELEGATES $func;
Branch: 1 branch
BranchTrueFalseTotal
$DELEGATES and print $DELEGATES $func 0 0 0
449 }
450 0 $func .= $class->_delegateAccess($memberPackage,$delegate,@interfaces);
451 # print "Compiling delegates:\n$func\n";
452 0 ▶ eval $func; $@ && die "Error compiling code:\n$func";
Branch: 1 branch
BranchTrueFalseTotal
$@ and die "Error compiling code:\n$func" 0 0 0
453 }
454 # now add that entry to @ISA
455 2 my %ISA = map { ($_,1) } @$isaref;
456 2 ▶ if(((%all or %$delegates) and not exists $ISA{$class."::memberAccess"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (%all || %$delegates and not exists $ISA{$class . '::memberAccess'}) 0 2 2
Condition33%2/6
%all || %$delegates and not exists $ISA{$class . '::memberAccess'}
!ll&&!rl&&r
200
%all or %$delegates
l!l&&r!l&&!r
002
MC/DC0%0/3
%all || %$delegates and not exists $ISA{$class . '::memberAccess'}
%all %$delegates not exists $ISA{$class . '::memberAccess'}
Truth table20%1/5
A or B and not(C)
A= %allB= %$delegatesC= not exists $ISA{$class . '::memberAccess'}
ABCresult
00X0
1X00
0100
1X11
0111
457 0 $memberPackage->MRP::BaseClass::check4Clashes();
458 0 unshift(@$isaref, $class."::memberAccess");
459 0 %allfunctions = (%allfunctions,%{$cache{$memberPackage}->{functions}});
460 }
461  
462 2 $classCache->{functions} = {%allfunctions};
463 2 $classCache->{fields} = {%allfields};
464 # print "$class has interface\n", MRP::Text->pretyHash(' ', %allfunctions), "\n";
465
466 2 $cache{$class} = $classCache;
467 2 ▶ close $VARIABLES if $debug{'variables'};
Branch: 1 branch
BranchTrueFalseTotal
if $debug{"variables"} 0 2 2
468 2 ▶ close $DELEGATES if $debug{'delegates'};
Branch: 1 branch
BranchTrueFalseTotal
if $debug{"delegates"} 0 2 2
469 # print "Checked $class\n";
470 }
471  
472 0 ▶sub _delegateFunc {
Subroutine _delegateFunc: not called CC=1 SCAR=6.9
473 0 my ($thingy, $memberPackage, $delegate, $function) = @_;
474  
475 0 return qq(
476 package $memberPackage;
477 sub $function {
478 my \$self = shift;
479 return \$self->$delegate->$function(\@_);
480 }
481 );
482 }
483  
484 0 ▶sub _delegateAccess {
Subroutine _delegateAccess: not called CC=1 SCAR=6.9
485 0 my ($thingy, $memberPackage, $item) = (shift,shift,shift);
486  
487 0 my $performChecks = join ";\n", map {qq(MRP::Interface->$_->implementedBy(\$value) or
488 Carp::confess "Delegate \$value must implement interface $_")
489 0 } map { $_->name() } @_;
490  
491 0 return qq(
492 package $memberPackage;
493 sub $item {
494 my \$self = shift;
495 if(\@_) {
496 my \$value = shift;
497 $performChecks;
498 return \$self->{'$item'} = \$value;
499 } else {
500 return \$self->{'$item'};
501 }
502 }
503 );
504 }
505  
506 0 ▶sub _fieldScalarFunc {
Subroutine _fieldScalarFunc: not called CC=3 SCAR=24.8
507 0 my ($thingy, $memberPackage, $name, $interface, $item) = @_;
508 0 ▶ $item ||= $name;
Condition0%0/3
$item ||= $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$item ||= $name
$item $name
Truth table0%0/3
A ||= B
A= $itemB= $name
ABresult
1X1
011
000
509  
510 0 my $interfaceText = "";
511 0 ▶ if(ref($interface)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $interface) 0 0 0
512 0 $interface = $interface->name;
513 0 $interfaceText = qq(MRP::Interface->$interface->implementedBy(\$_[0]) or
514 Carp::confess "field \$_[0] must implement interface $interface";);
515  
516 }
517  
518 0 return qq(
519 package $memberPackage;
520 sub $name {
521 my \$self = shift;
522 (\@_)
523 ? do { $interfaceText
524 \$self->{'$item'} = \$_[0];
525 }
526 : \$self->{'$item'};
527 }
528 );
529 }
530  
531 0 ▶sub _packageScalarFunc {
Subroutine _packageScalarFunc: not called CC=2 SCAR=17.9
532 0 my ($thingy, $memberPackage, $name, $item) = @_;
533 0 ▶ $item ||= $name;
Condition0%0/3
$item ||= $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$item ||= $name
$item $name
Truth table0%0/3
A ||= B
A= $itemB= $name
ABresult
1X1
011
000
534 0 my $packagevar = $thingy.'::'.$item;
535  
536 0 return qq(
537 package $memberPackage;
538 sub $name {
539 my \$class = shift;
540 scalar(\@_)
541 ? \$$packagevar = \$_[0]
542 : \$$packagevar;
543 }
544 );
545 }
546  
547 0 ▶sub _fieldArrayFunc {
Subroutine _fieldArrayFunc: not called CC=2 SCAR=17.9
548 0 my ($thingy, $memberPackage, $name, $item) = @_;
549 0 ▶ $item ||= $name;
Condition0%0/3
$item ||= $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$item ||= $name
$item $name
Truth table0%0/3
A ||= B
A= $itemB= $name
ABresult
1X1
011
000
550 0 my $name_ref = $name.'_ref';
551  
552 0 return qq(
553 package $memberPackage;
554 sub $name {
555 my \$self = shift;
556 if (\@_ or not defined wantarray) {
557 \$self->{'$item'} = [] if not defined \$self->{'$item'};
558 \@{\$self->{'$item'}} = \@_;
559 }
560 my \$ret = \$self->{'$item'};
561 return ref(\$ret)
562 ? (wantarray)
563 ? \@{\$self->{'$item'}}
564 : \$self->{'$item'}
565 : (not defined \$ret)
566 ? undef
567 : &Carp::confess("Ilegal value for $item in \$self\n".\$self->_printMembers);
568 }
569 ) . $thingy->_fieldScalarFunc($memberPackage, $name_ref, undef, $item);
570 }
571  
572 0 ▶sub _packageArrayFunc {
Subroutine _packageArrayFunc: not called CC=2 SCAR=17.9
573 0 my ($thingy, $memberPackage, $name, $item) = @_;
574 0 ▶ $item ||= $name;
Condition0%0/3
$item ||= $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$item ||= $name
$item $name
Truth table0%0/3
A ||= B
A= $itemB= $name
ABresult
1X1
011
000
575 0 my $packagevar = $thingy.'::'.$item;
576  
577 0 return qq(
578 package $memberPackage;
579 sub $name {
580 my \$class = shift;
581 \@$packagevar = \@_ if (\@_) or not defined wantarray;
582 wantarray
583 ? \@$packagevar
584 : \\\@$packagevar;
585 }
586 );
587 }
588  
589 0 ▶sub _fieldHashFunc {
Subroutine _fieldHashFunc: not called CC=2 SCAR=17.9
590 0 my ($thingy, $memberPackage, $name, $item) = @_;
591 0 ▶ $item ||= $name;
Condition0%0/3
$item ||= $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$item ||= $name
$item $name
Truth table0%0/3
A ||= B
A= $itemB= $name
ABresult
1X1
011
000
592 0 my $name_ref = $item.'_ref';
593  
594 0 return qq(
595 package $memberPackage;
596 sub $name {
597 my \$self = shift;
598 if(\@_ or not defined wantarray) {
599 if(\@_==1) {
600 my \$val = shift;
601 if(ref \$val eq 'HASH') {
602 %{\$self->{'$item'}} = %\$val;
603 } elsif (ref \$_[0] eq 'ARRAY') {
604 %{\$self->{'$item'}} = \@\$val;
605 } else {
606 Carp::confess "Can not set the hash member variable '$item' to \$val";
607 }
608 } else {
609 %{\$self->{'$item'}} = \@_;
610 }
611 }
612 my \$ret = \$self->{'$item'};
613 return (ref \$ret)
614 ? (wantarray)
615 ? (%{\$self->{'$item'}})
616 : (\$self->{'$item'})
617 : not defined(\$ret)
618 ? undef
619 : Carp::confess "Member $item of \$self has gained the ilegal value '\$ret'" . \$self->_printMembers;
620 }
621 ) . $thingy->_fieldScalarFunc($memberPackage, $name_ref, undef, $item);
622 }
623  
624 0 ▶sub _packageHashFunc {
Subroutine _packageHashFunc: not called CC=2 SCAR=17.9
625 0 my ($thingy, $memberPackage, $name, $item) = @_;
626 0 ▶ $item ||= $name;
Condition0%0/3
$item ||= $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$item ||= $name
$item $name
Truth table0%0/3
A ||= B
A= $itemB= $name
ABresult
1X1
011
000
627 0 my $packagevar = $thingy.'::'.$item;
628  
629 0 return qq(
630 package $memberPackage;
631 sub $name {
632 my \$class = shift;
633 \%$packagevar = \@_ if (\@_ or not defined wantarray);
634 wantarray
635 ?\%$packagevar
636 : \\%$packagevar;
637 }
638 );
639 }
640  
641 0 ▶sub _packageAndField {
Subroutine _packageAndField: not called CC=1 SCAR=6.9
642 0 my ($thingy,$memberPackage,$item,$fieldFunc,$packageFunc) = @_;
643  
644 0 return qq(
645 package $memberPackage;
646 sub $item {
647 my \$thingy = shift;
648 ref(\$thingy)
649 ? \$thingy->$fieldFunc(\@_)
650 : \$thingy->$packageFunc(\@_);
651 }
652 );
653 }
654  
655 0 ▶sub _defaultField {
Subroutine _defaultField: not called CC=1 SCAR=6.9
656 0 my ($thingy,$memberPackage,$item,$fieldFunc,$packageFunc) = @_;
657  
658 0 return qq(
659 package $memberPackage;
660 sub $item {
661 my \$thingy = shift;
662 my \@return;
663 ref(\$thingy)
664 ? do {
665 \@return = (\$thingy->$fieldFunc(\@_));
666 (\@return &&
667 not (\@return == 1 && not defined(\$return[0])))
668 ? \@return
669 : \$thingy->$packageFunc(\@_)
670 }
671 : \$thingy->$packageFunc(\@_);
672 }
673 );
674 }
675  
676 1 ▶use vars qw(%builtInRefs);
Subroutine BEGIN: called CC=1 SCAR=0.0
677 1 %builtInRefs = map { ($_,1) } qw(REF SCALAR ARRAY HASH CODE GLOB);
678  
679 0 ▶sub isObject {
Subroutine isObject: not called CC=3 SCAR=24.8
Pod undocumented
680 0 my $self = shift;
681 0 my $ref = shift;
682 0 ▶ $ref = ref($ref) || return undef;
Condition0%0/2
ref $ref || (return undef)
l!l
00
MC/DC0%0/1
ref $ref || (return undef)
ref $ref
Truth table0%0/2
A
A= ref $ref
Aresult
11
00
683 0 ▶ exists $builtInRefs{$ref} && return undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $MRP::BaseClass::builtInRefs{$ref} and return undef 0 0 0
684 0 return $ref;
685 }
686  
687 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
688 1 ▶ use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
689 1 @ISA = qw(Exporter);
690 1 MRP::BaseClass->check4Clashes();
691 }
692  
693 1 $VERSION; # says use was ok
694 __END__
↑ Summary Interface.pm »