Object/Common.pm

filter: Statementstmt 84.2% 112 / 133 Branchbran 71.3% 87 / 122 Conditioncond 68.1% 15 / 22 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 3 totaltotal 74.9% 227 / 303 CC 79 SCAR 39.9
CC
79
Cov
77%
FetchParams
78.0
LoadData
25.5
SaveData
13.7
CRAP 54.2
↑ Summary Object.pm »
1 package Games::Object::Common;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Carp qw(carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 ▶use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 $VERSION = "0.10";
11 10 @ISA = qw(Exporter);
12 10 @EXPORT_OK = qw(ANAME_MANAGER FetchParams LoadData SaveData);
13 10 %EXPORT_TAGS = (
14 attrnames => [qw(ANAME_MANAGER)],
15 functions => [qw(FetchParams LoadData SaveData)],
16 );
17  
18 10 ▶use constant ANAME_MANAGER => "_MANAGER";
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 ####
21 ## PUBLIC FUNCTIONS
22  
23 # Save an item of data to a file.
24  
25 sub SaveData
26 769 ▶{
Subroutine SaveData: called CC=11 SCAR=26.2
Pod undocumented
27 769 my ($file, $data) = @_;
28  
29 # Check for undef, as this takes special handling.
30 769 ▶ if (!defined($data)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 769 769
31 0 print $file "U\n";
32 0 return 1;
33 }
34  
35 # Now handle everything else.
36 769 my $ref = ref($data);
37 769 ▶ if ($ref eq '' && $data =~ /\n/) {
Branch: 5 branches
BranchTrueFalseTotal
if ($ref eq '' and $data =~ /\n/) { } 0 769 769
elsif ($ref eq '') { } 534 235 769
elsif ($ref eq 'ARRAY') { } 50 185 235
elsif ($ref eq 'HASH') { } 176 9 185
elsif ($ref and UNIVERSAL::can($ref, 'save')) { } 9 0 9
Condition66%2/3
$ref eq "" and $data =~ /\n/
!ll&&!rl&&r
2355340
MC/DC0%0/2
$ref eq "" and $data =~ /\n/
$ref eq "" $data =~ /\n/
Truth table66%2/3
A and B
A= $ref eq ""B= $data =~ /\n/
ABresult
0X0
100
111
38 # Multiline text scalar
39 0 my @lines = split(/\n/, $data);
40 0 print $file "M " . scalar(@lines) . "\n" .
41 join("\n", @lines) . "\n";
42 } elsif ($ref eq '') {
43 # Simple scalar.
44 534 print $file "S $data\n";
45 } elsif ($ref eq 'ARRAY') {
46 # Array.
47 50 print $file "A " . scalar(@$data) . "\n";
48 50 foreach my $item (@$data) {
49 94 SaveData($file, $item);
50 }
51 } elsif ($ref eq 'HASH') {
52 # Hash. WARNING: Hash keys cannot have newlines in them!
53 176 my @keys = keys %$data;
54 176 print $file "H " . scalar(@keys) . "\n";
55 176 foreach my $key (@keys) {
56 655 print $file "$key\n";
57 655 SaveData($file, $data->{$key});
58 }
59 ▶ } elsif ($ref && UNIVERSAL::can($ref, 'save')) {
Condition33%1/3
$ref and UNIVERSAL::can($ref, "save")
!ll&&!rl&&r
009
MC/DC0%0/2
$ref and UNIVERSAL::can($ref, "save")
$ref UNIVERSAL::can($ref, "save")
Truth table33%1/3
A and B
A= $refB= UNIVERSAL::can($ref, "save")
ABresult
0X0
100
111
60 # Pass along to save method of this object's class.
61 9 print $file "O $ref\n";
62 9 $data->save($file);
63 } else {
64 # SOL
65 0 croak("Cannot save reference to $ref object");
66 }
67 769 1;
68 }
69  
70 # Load data from a file. This can take an optional second parameter. If present,
71 # this is taken to be a reference to a variable that will hold the data, rather
72 # than creating our own and returning the result (this applies only to
73 # non-scalar data). WARNING!! No check is made to insure that the reference
74 # type is compatible with what is in the file!
75  
76 sub LoadData
77 874 ▶{
Subroutine LoadData: called CC=16 SCAR=32.4
Pod undocumented
78 874 my ($file, $store) = @_;
79 874 my $line = <$file>;
80  
81 # The caller is responsible for calling this routine only when there
82 # is data to read.
83 874 ▶ croak("Unexpected EOF") if (!defined($line));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 874 874
84  
85 # Check for something we recognize.
86 874 chomp $line;
87 874 my $tag = substr($line, 0, 1);
88 874 ▶ my $val = substr($line, 2) if ($tag ne 'U'); # Avoid substr warning
Branch: 1 branch
BranchTrueFalseTotal
if $tag ne "U" 874 0 874
89 874 ▶ if ($tag eq 'U') {
Branch: 6 branches
BranchTrueFalseTotal
if ($tag eq 'U') { } 0 874 874
elsif ($tag eq 'S') { } 610 264 874
elsif ($tag eq 'M') { } 0 264 264
elsif ($tag eq 'A') { } 58 206 264
elsif ($tag eq 'H') { } 196 10 206
elsif ($tag eq 'O') { } 10 0 10
90 # Undef.
91 0 undef;
92 } elsif ($tag eq 'S') {
93 # Simple scalar value
94 610 $val;
95 } elsif ($tag eq 'M') {
96 # Multiline text, to be returned as scalar.
97 0 my @text = ();
98 0 foreach my $i (1 .. $val) {
99 0 my $line2 = <$file>;
100 0 ▶ croak("Unexpected EOF") if (!defined($line2));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line2 0 0 0
101 0 push @text, $line2;
102 }
103 0 join("\n", @text);
104 } elsif ($tag eq 'A') {
105 # Build an array.
106 58 ▶ my $ary = $store || [];
Condition50%1/2
$store || []
l!l
058
MC/DC0%0/1
$store || []
$store
Truth table50%1/2
A
A= $store
Aresult
11
00
107 58 foreach my $i (1 .. $val) {
108 115 push @$ary, LoadData($file);
109 }
110 58 $ary;
111 } elsif ($tag eq 'H') {
112 # Reconstruct a hash.
113 196 ▶ my $hsh = $store || {};
Condition100%2/2
$store || {}
l!l
21175
MC/DC100%1/1
$store || {}
$store
Truth table100%2/2
A
A= $store
Aresult
11
00
114 196 foreach my $i (1 .. $val) {
115 738 my $key = <$file>;
116 738 chomp $key;
117 738 $hsh->{$key} = LoadData($file);
118 }
119 196 $hsh;
120 } elsif ($tag eq 'O') {
121 # Object reference. We first make sure this has the proper method
122 # and then call it.
123 10 ▶ if (UNIVERSAL::can($val, 'load')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($val, 'load')) { } 10 0 10
124 10 my $obj = $val->load($file);
125 10 $obj;
126 } else {
127 0 croak "Cannot load object of class '$val' (no load method)";
128 }
129 } else {
130 # Anything else is unrecognized.
131 0 croak("Unknown tag '$tag' in file, file may be corrupted");
132 }
133  
134 }
135  
136 # Fetch parameters, checking for required params and validating the values.
137  
138 sub FetchParams
139 1421 ▶{
Subroutine FetchParams: called CC=53 SCAR=43.6
Pod undocumented
140 1421 my ($args, $res, $opts, $del) = @_;
141 1421 ▶ $del = 0 if (!defined($del));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $del 1051 370 1421
142  
143 # If the first item is the name of this class, shift it off.
144 1421 ▶ shift @$args if (@$args && $args->[0] =~ /^Games::Object/);
Branch: 1 branch
BranchTrueFalseTotal
if @$args and $args->[0] =~ /^Games::Object/ 0 1421 1421
Condition66%2/3
@$args and $args->[0] =~ /^Games::Object/
!ll&&!rl&&r
3513860
MC/DC0%0/2
@$args and $args->[0] =~ /^Games::Object/
@$args $args->[0] =~ /^Games::Object/
Truth table66%2/3
A and B
A= @$argsB= $args->[0] =~ /^Games::Object/
ABresult
0X0
100
111
145  
146 # Now go down the opts list and see what parameters are needed.
147 # Return the results in a hash.
148 1421 my %args = @$args;
149 1421 ▶ while (my $spec = shift @$opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $spec = shift @$opts) 6868 1421 8289
150  
151 # Fetch the values for this spec. Note that not all may be present,
152 # depending on the type.
153 6868 my ($type, $name, $dflt, $rstr) = @$spec;
154  
155 # Philosophy conflict: Many CPAN modules like args to be passed
156 # with '-' prefixing them. I don't. Useless use of an extra
157 # keystroke. However, I want to be consistent. Thus a compromise:
158 # I allow args to be passed with or without the '-', but it always
159 # gets stored internally without the '-'.
160 6868 my $oname = $name;
161 6868 ▶ $name = '-' . $name if (defined($args{"-${name}"}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"-$name"} 693 6175 6868
162  
163 # Is the attribute name a pattern? If so, here's what we do: we
164 # search the list of args for attribute names that match this
165 # and automagically generate specific options that we tack on
166 # to the end of the list.
167 6868 ▶ if ($name =~ /[\^\$\.\+\*\[\{]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /[\^\$\.\+\*\[\{]/) 126 6742 6868
168 104 my @amatches = grep { /$name/ }
169 126 map { s/^\-//g; $_; }
170 keys %args;
171 126 foreach my $amatch (@amatches) {
172 16 push @$opts, [ $type, $amatch, $dflt, $rstr ];
173 }
174 126 next;
175 }
176  
177 # Check the type.
178 6742 ▶ if ($type eq 'req') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'req') { } 1085 5657 6742
elsif ($type eq 'opt') { } 5657 0 5657
179  
180 # Required parameter, so it must be provided.
181 croak("Missing required argument '$name'")
182 1085 ▶ unless (defined($args{$name}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{$name} 0 1085 1085
183 1085 $res->{$oname} = $args{$name};
184  
185 } elsif ($type eq 'opt') {
186  
187 # Optional parameter. If not there and a default is specified,
188 # then set it to that.
189 5657 ▶ if (defined($args{$name})) { $res->{$oname} = $args{$name}; }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{$name}) { } 2407 3250 5657
elsif (defined $dflt) { } 1061 2189 3250
190 1061 elsif (defined($dflt)) { $res->{$oname} = $dflt; }
191  
192 }
193  
194 # Delete item from args if requested.
195 6742 ▶ delete $args{$name} if ($del);
Branch: 1 branch
BranchTrueFalseTotal
if $del 1520 5222 6742
196  
197 # Stop here if we wound up with undef anyway or there are no
198 # restrictions on the parameter.
199 6742 ▶ next if (!defined($res->{$oname}) || !defined($rstr));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res->{$oname} and defined $rstr 2408 4334 6742
Condition100%3/3
defined $res->{$oname} and defined $rstr
!ll&&!rl&&r
21892194334
MC/DC100%2/2
defined $res->{$oname} and defined $rstr
defined $res->{$oname} defined $rstr
Truth table100%3/3
A and B
A= defined $res->{$oname}B= defined $rstr
ABresult
0X0
100
111
200  
201 # Check for additional restrictions.
202 4334 ▶ if (ref($rstr) eq 'CODE') {
Branch: 12 branches
BranchTrueFalseTotal
if (ref $rstr eq 'CODE') { } 260 4074 4334
elsif (ref $rstr eq 'ARRAY') { } 231 3843 4074
elsif ($rstr eq 'any') { } 402 3441 3843
elsif ($rstr =~ /^(.+)ref$/) { } 529 2912 3441
elsif ($rstr eq 'int') { } 1024 1888 2912
elsif ($rstr eq 'number') { } 99 1789 1888
elsif ($rstr eq 'boolean') { } 429 1360 1789
elsif ($rstr eq 'string') { } 602 758 1360
elsif ($rstr eq 'callback') { } 234 524 758
elsif ($rstr eq 'file') { } 36 488 524
elsif ($rstr eq 'readable_filename') { } 0 488 488
elsif ($rstr eq 'object') { } 488 0 488
203  
204 # User defining own validation code.
205 croak("Invalid value '$res->{$oname}' for param '$name'")
206 260 ▶ if (! &$rstr($res->{$oname}) );
Branch: 1 branch
BranchTrueFalseTotal
unless &$rstr($res->{$oname}) 0 260 260
207  
208 } elsif (ref($rstr) eq 'ARRAY') {
209  
210 # Value must be one of these
211 231 my $found = 0;
212 231 foreach my $item (@$rstr) {
213 388 $found = ( $item eq $res->{$oname} );
214 388 ▶ last if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 231 157 388
215 }
216 231 ▶ croak("Invalid value '$res->{$oname}' for param '$name'")
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 231 231
217 unless ($found);
218  
219 } elsif ($rstr eq 'any') {
220  
221 # Automatically succeeds.
222  
223 } elsif ($rstr =~ /^(.+)ref$/) {
224  
225 529 my $reftype = uc($1);
226 croak("Parameter '$name' must be $reftype ref")
227 529 ▶ if (ref($res->{$oname}) ne $reftype);
Branch: 1 branch
BranchTrueFalseTotal
if ref $res->{$oname} ne $reftype 0 529 529
228  
229 } elsif ($rstr eq 'int') {
230  
231 # Must be an integer.
232 croak("Param '$name' must be an integer")
233 1024 ▶ if ($res->{$oname} !~ /^[\+\-\d]\d*$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{$oname} =~ /^[\+\-\d]\d*$/ 0 1024 1024
234  
235 } elsif ($rstr eq 'number') {
236  
237 # Must be a number. Rather than trying to match against a
238 # heinously long regexp, we'll intercept the warning for
239 # a non-numeric when we try to int() it. TMTOWTDI.
240 99 my $not_number = 0;
241 0 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
242 0 my $msg = shift;
243 0 ▶ if ($msg =~ /isn't numeric in int/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg =~ /isn't numeric in int/) { } 0 0 0
244 0 $not_number = 1;
245 } else {
246 0 warn $msg;
247 }
248 99 };
249 99 my $x = int($res->{$oname});
250 99 ▶ croak("Param '$name' must be a number") if ($not_number);
Branch: 1 branch
BranchTrueFalseTotal
if $not_number 0 99 99
251  
252 } elsif ($rstr eq 'boolean') {
253  
254 # Must be a boolean. We simply convert to a 0 or 1.
255 my $bool = ( $res->{$oname} eq '0' ? 0 :
256 429 ▶ $res->{$oname} eq '' ? 0 :
Branch: 2 branches
BranchTrueFalseTotal
$res->{$oname} eq '0' ? : 383 46 429
$res->{$oname} eq '' ? : 0 46 46
257 1 );
258 429 $res->{$oname} = $bool;
259  
260 } elsif ($rstr eq 'string') {
261  
262 # Must not be a reference
263 croak("Param '$name' must be a string, not a reference")
264 602 ▶ if (ref($res->{$oname}));
Branch: 1 branch
BranchTrueFalseTotal
if ref $res->{$oname} 0 602 602
265  
266 } elsif ($rstr eq 'callback') {
267  
268 # Must be a callback definition, which is minimally an
269 # array with two items. Note that we can have lists of
270 # callbacks as well; so if this is not already such a list,
271 # make it one with a single entry for the purposes of checking
272 # it here.
273 234 my $list = $res->{$oname};
274 234 ▶ croak "Param '$name' must be a callback array or list of " .
Branch: 1 branch
BranchTrueFalseTotal
if ref $list ne "ARRAY" 0 234 234
275 "callback arrays" if (ref($list) ne 'ARRAY');
276 234 ▶ $list = [ $list ]
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 0 or ref $list->[0] ne "ARRAY" 190 44 234
Condition66%2/3
@$list == 0 or ref $list->[0] ne "ARRAY"
l!l&&r!l&&!r
019044
MC/DC50%1/2
@$list == 0 or ref $list->[0] ne "ARRAY"
@$list == 0 ref $list->[0] ne "ARRAY"
Truth table66%2/3
A or B
A= @$list == 0B= ref $list->[0] ne "ARRAY"
ABresult
1X1
011
000
277 if (@$list == 0 || ref($list->[0]) ne 'ARRAY');
278 234 foreach my $cbk (@$list) {
279 327 ▶ next if (!ref($cbk) && $cbk eq 'FAIL');
Branch: 1 branch
BranchTrueFalseTotal
if not ref $cbk and $cbk eq "FAIL" 7 320 327
Condition66%2/3
not ref $cbk and $cbk eq "FAIL"
!ll&&!rl&&r
32007
MC/DC50%1/2
not ref $cbk and $cbk eq "FAIL"
not ref $cbk $cbk eq "FAIL"
Truth table66%2/3
not(A) and B
A= not ref $cbkB= $cbk eq "FAIL"
ABresult
0X0
100
111
280 320 ▶ croak "Param '$name' must be a callback or list of " .
Branch: 1 branch
BranchTrueFalseTotal
if ref $cbk ne "ARRAY" 0 320 320
281 "callbacks" if (ref($cbk) ne 'ARRAY');
282 320 ▶ croak "Param '$name' callback must contain at least two " .
Branch: 1 branch
BranchTrueFalseTotal
if @$cbk < 2 0 320 320
283 "parameters" if (@$cbk < 2);
284 320 foreach my $item (@$cbk) {
285 1578 ▶ croak "Param '$name' callback args must be simple " .
Branch: 1 branch
BranchTrueFalseTotal
if ref $item 0 1578 1578
286 "scalars" if (ref($item));
287 }
288 }
289  
290 } elsif ($rstr eq 'file') {
291  
292 # Must be reference to an IO::File or FileHandle object, or
293 # a GLOB.
294 croak("Param '$name' must be a file (IO::File/" .
295 "FileHandler object or GLOB reference acceptable)")
296 36 ▶ if (ref($res->{$oname}) !~ /^(IO::File|FileHandle|GLOB)$/);
Branch: 1 branch
BranchTrueFalseTotal
unless ref($res->{$oname}) =~ /^(IO::File|FileHandle|GLOB)$/ 0 36 36
297  
298 } elsif ($rstr eq 'readable_filename' ) {
299  
300 # Must be the name of a file that exists and is readable.
301 croak("Filename '$res->{$oname}' does not exist")
302 0 ▶ if (! -f $res->{$oname});
Branch: 1 branch
BranchTrueFalseTotal
unless -f $res->{$oname} 0 0 0
303 croak("Filename '$res->{$oname}' is not readable")
304 0 ▶ if (! -r $res->{$oname});
Branch: 1 branch
BranchTrueFalseTotal
unless -r $res->{$oname} 0 0 0
305  
306 } elsif ($rstr eq 'object') {
307  
308 # Must be an object reference
309 488 my $ref = ref($res->{$oname});
310 488 ▶ croak("Param '$name' must be an object reference, not a " .
Branch: 1 branch
BranchTrueFalseTotal
if $ref =~ /^(SCALAR|ARRAY|HASH|CODE|REF|GLOB|LVALUE)$/ 0 488 488
311 "'$ref' reference")
312 if ($ref =~ /^(SCALAR|ARRAY|HASH|CODE|REF|GLOB|LVALUE)$/);
313 } else {
314  
315 0 croak("'$rstr' is an invalid datatype");
316  
317 }
318 }
319  
320 # Set args to trimmed amount if delete option requested.
321 1421 ▶ @$args = %args if ($del);
Branch: 1 branch
BranchTrueFalseTotal
if $del 370 1051 1421
322  
323 1421 $res;
324 }
325  
326 10 1;
↑ Summary Object.pm »