PartialOrder/Loader.pm

filter: Statementstmt 58.9% 151 / 256 Branchbran 44.3% 55 / 124 Conditioncond 32.2% 20 / 62 MC/DCmcdc 10.2% 4 / 39 Subroutinesub 69.2% 18 / 26 Podpod 0.0% 0 / 10 totaltotal 47.9% 248 / 517 CC 91 SCAR 37.4
CC
91
Cov
51%
_retrieve_head
158.6
graphviz
72.0
from_perl_isa
30.0
CRAP 42.0
« PartialOrder/CMasked.pm ↑ Summary PartialOrder/Base.pm »
1 # -*- Mode: Perl -*-
2  
3 #
4 # Copyright (c) 2001, Bryan Jurish. All rights reserved.
5 #
6 # This package is free software. You may redistribute it
7 # and/or modify it under the same terms as Perl itself.
8 #
9  
10 ###############################################################
11 #
12 # File: Math::PartialOrder::Loader.pm
13 # Author: Bryan Jurish <jurish@ling.uni-potsdam.de>
14 #
15 # Description: Load QuD Hierarchies from files
16 #
17 ###############################################################
18  
19  
20 package Math::PartialOrder::Loader;
21 7 ▶use Math::PartialOrder::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 7 @ISA = qw(Exporter);
23 7 @EXPORT = qw();
24 7 @EXPORT_OK = qw($_tr_name $_tr_parents $_tr_attrs);
25 7 %EXPORT_TAGS = (
26 trvars => [qw($_tr_name $_tr_parents $_tr_attrs)],
27 );
28  
29 7 our $VERSION = 0.01;
30 7 our $BIN_COMPAT = 0.01;
31 7 our ($_tr_name, $_tr_parents, $_tr_attrs) = (0..2);
32  
33 7 our $PS_VIEWER = 'gv';
34 7 our @TMPFILES = qw();
35 7 our $UNLINK_TMPFILES = 1;
36  
37 package Math::PartialOrder::Base;
38 7 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 7 ▶eval "use Storable qw();";
Subroutine BEGIN: called CC=1 SCAR=0.0
40 0 ▶eval "use GraphViz;";
Subroutine BEGIN: called CC=1 SCAR=1.2
41 7 ▶eval "use File::Temp;";
Subroutine BEGIN: called CC=1 SCAR=0.0
42 7 Math::PartialOrder::Loader->import(q(:trvars));
43  
44 ###############################################################
45 # Constants
46 ###############################################################
47 # None
48  
49 ###############################################################
50 # perl-load
51 ###############################################################
52  
53 # usage: add_perl_class($class,%opts);
54 # + options:
55 # descend=>$bool
56 0 ▶sub from_perl_isa {
Subroutine from_perl_isa: not called CC=5 SCAR=34.0
Pod undocumented
57 0 my ($h,$class,%opts) = @_;
58 0 my %seen = ();
59 0 ▶ $opts{descend} = 1 unless (defined($opts{descend}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"descend"} 0 0 0
60 0 ▶ if ($opts{descend}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'descend'}) { } 0 0 0
61 0 my @q = ($class);
62 0 ▶ while (defined($class = shift(@q))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($class = shift @q)) 0 0 0
63 0 ▶ next if (exists($seen{$class}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen{$class} 0 0 0
64 0 $seen{$class} = undef;
65 0 $h->add($class, @{$class.'::ISA'});
66 0 push(@q, grep { !exists($seen{$_}) } @{$class.'::ISA'});
67 }
68 } else {
69 # just add this class
70 0 $h->add($class,@{$class.'::ISA'});
71 }
72 }
73  
74  
75 ###############################################################
76 # Text-Load
77 ###############################################################
78  
79 #----------------------------------------------------------------------
80 # Public loading functions:
81 # load($filename,\%opts) => undef
82 # load($FileHandle,\%opts) => undef
83 # + loads in hierarchy from $filename or $FileHandle,
84 # wiping any existant data
85 # Private loading functions:
86 # _load($FileHandle,\%opts) => undef
87 # + top-level do the work of loading
88 #----------------------------------------------------------------------
89 5 ▶sub load {
Subroutine load: called CC=3 SCAR=11.1
Pod undocumented
90 5 my $self = shift;
91 5 my $file = shift;
92 5 ▶ return $self->_load($file,@_) if (ref($file));
Branch: 1 branch
BranchTrueFalseTotal
if ref $file 0 5 5
93  
94 5 my $handle = FileHandle->new("<$file");
95 5 ▶ croak("open failed for `$file': $!") unless (defined($handle));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $handle 0 5 5
96 5 my $rv = $self->_load($handle, @_);
97 5 $handle->close();
98 5 return $rv;
99 }
100  
101 5 ▶sub _load {
Subroutine _load: called CC=13 SCAR=26.5
102 5 my ($h,$fh,$args) = @_;
103 5 ▶ $args = {} unless (defined($args));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 5 0 5
104  
105 # actual loading
106 5 my ($gtline,$subx,$sub,$super,@supers,%subattrs,$attref);
107 5 ▶ $args->{gtsep} = '>' unless (defined($args->{gtsep}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"gtsep"} 5 0 5
108 # read in the hierarchy
109 5 ▶ while ($gtline = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($gtline = readline $fh)) 65 5 70
110 65 chomp($gtline);
111 65 ▶ if ($gtline =~ /^\s*root\s*=\s*(.*)/i && $1 ne $h->root) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gtline =~ /^\s*root\s*=\s*(.*)/i and $1 ne $h->root) 5 60 65
Condition66%2/3
$gtline =~ /^\s*root\s*=\s*(.*)/i and $1 ne $h->root
!ll&&!rl&&r
6005
MC/DC50%1/2
$gtline =~ /^\s*root\s*=\s*(.*)/i and $1 ne $h->root
$gtline =~ /^\s*root\s*=\s*(.*)/i $1 ne $h->root
Truth table66%2/3
A and B
A= $gtline =~ /^\s*root\s*=\s*(.*)/iB= $1 ne $h->root
ABresult
0X0
100
111
112 5 $h->replace($h->root,$1);
113 5 next;
114 }
115 60 ▶ next unless ($gtline !~ /^\s*\#/ && $gtline !~ /^\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
if $gtline =~ /^\s*\#/ or $gtline =~ /^\s*$/ 20 40 60
Condition66%2/3
$gtline =~ /^\s*\#/ or $gtline =~ /^\s*$/
l!l&&r!l&&!r
20040
MC/DC50%1/2
$gtline =~ /^\s*\#/ or $gtline =~ /^\s*$/
$gtline =~ /^\s*\#/ $gtline =~ /^\s*$/
Truth table66%2/3
A or B
A= $gtline =~ /^\s*\#/B= $gtline =~ /^\s*$/
ABresult
1X1
011
000
116 40 ($subx, $super) = split(/\s+(?:$args->{gtsep})\s+/, $gtline);
117  
118 40 ($sub, %subattrs) = split(/\s*[\[\]:,]\s*/, $subx);
119 40 $sub =~ s/^\s*(\S+)/$1/;
120 40 ▶ if (defined($super)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $super) { } 35 5 40
121 35 $super =~ s/^(\S+)\s*/$1/;
122 35 @supers = split(/\s*,\s*/, $super);
123 } else {
124 5 @supers = qw();
125 }
126  
127 40 ▶ if (!$h->has_type($sub)) { # We found a new subtype...
Branch: 1 branch
BranchTrueFalseTotal
if (not $h->has_type($sub)) { } 25 15 40
128 25 $h->add($sub, @supers);
129 }
130 else {
131 # subtype already defined -- add/override attributes
132 15 $attref = $h->_attributes($sub);
133 }
134  
135 # now, set the attributes
136 40 ▶ if (%subattrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (%subattrs) 0 40 40
137 0 ▶ $attref = $h->_attributes($sub) || {};
Condition0%0/2
$h->_attributes($sub) || {}
l!l
00
MC/DC0%0/1
$h->_attributes($sub) || {}
$h->_attributes($sub)
Truth table0%0/2
A
A= $h->_attributes($sub)
Aresult
11
00
138 0 %$attref = (%$attref, %subattrs);
139 0 $h->_attributes($sub, $attref);
140 }
141  
142 40 ▶ if (@supers) { # HACK!
Branch: 1 branch
BranchTrueFalseTotal
if (@supers) 35 5 40
143 35 $h->move($sub, @supers);
144 }
145 #elsif (! grep { $_ eq $sub } $h->root()) {
146 # warn("Undefined supertype ",
147 # (defined($super) ? "'$super'" : ''),
148 # " for subtype '$sub' during text load.\n");
149 #}
150 }
151 5 return $h;
152 }
153  
154  
155 ##############################################################
156 # Text-Save
157 ###############################################################
158  
159 #----------------------------------------------------------------------
160 # Public saving functions:
161 # save($filename) => undef
162 # save($FileHandle) => undef
163 # Private saving functions:
164 # _save($FileHandle) => undef
165 #----------------------------------------------------------------------
166 5 ▶sub save {
Subroutine save: called CC=3 SCAR=11.2
Pod undocumented
167 5 my $self = shift;
168 5 my $file = shift;
169 5 ▶ return $self->_save($file,@_) if (ref($file));
Branch: 1 branch
BranchTrueFalseTotal
if ref $file 0 5 5
170  
171 5 my $handle = FileHandle->new(">$file");
172 5 ▶ croak("open failed for `$file': $!") unless (defined($handle));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $handle 0 5 5
173 5 $self->_save($handle,@_);
174 5 $handle->close();
175 }
176  
177 5 ▶sub _save {
Subroutine _save: called CC=8 SCAR=22.9
178 5 my ($h,$fh,$args) = @_;
179 5 ▶ $args = {} unless (defined($args));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 5 0 5
180  
181 # actual save
182 5 my ($sub,@supers,$super,$attrs);
183 5 ▶ print $fh ("# Save-file auto-generated by ", __PACKAGE__ , "::save\n",
Condition33%1/3
ref $h || $h
l!l&&r!l&&!r
500
MC/DC0%0/2
ref $h || $h
ref $h $h
Truth table33%1/3
A || B
A= ref $hB= $h
ABresult
1X1
011
000
184 "# Hierarchy Class = ", ref($h) || $h, "\n",
185 "# Hierarchy Version = ", $h->VERSION, "\n",
186 "# Loader Version = ", Math::PartialOrder::Loader->VERSION, "\n",
187 "ROOT=",$h->root,"\n");
188  
189 5 foreach $sub ($h->types) {
190 40 @supers = $h->parents($sub);
191 40 $attrs = $h->_attributes($sub);
192 print $fh ("$sub [",
193 (defined($attrs)
194 0 ▶ ? join(',', map { "$_:$attrs->{$_}" } keys(%$attrs))
Branch: 1 branch
BranchTrueFalseTotal
$sub ne $h->root && @supers ? : 35 5 40
Condition66%2/3
$sub ne $h->root and @supers
!ll&&!rl&&r
5035
MC/DC50%1/2
$sub ne $h->root and @supers
$sub ne $h->root @supers
Truth table66%2/3
A and B
A= $sub ne $h->rootB= @supers
ABresult
0X0
100
111
195 : qw()),
196 "]",
197 $sub ne $h->root && @supers ?
198 (
199 "\t >\t ",
200 join(',',
201 grep {
202 40 ▶ defined($_) && $h->has_type($_)
Branch: 1 branch
BranchTrueFalseTotal
defined $attrs ? : 0 40 40
Condition33%1/3
defined $_ && $h->has_type($_)
!ll&&!rl&&r
0040
MC/DC0%0/2
defined $_ && $h->has_type($_)
defined $_ $h->has_type($_)
Truth table33%1/3
A && B
A= defined $_B= $h->has_type($_)
ABresult
0X0
100
111
203 } @supers)
204 ) : qw(),
205 "\n");
206 }
207 5 return $h;
208 }
209  
210  
211  
212 ##############################################################
213 # Visualization
214 ###############################################################
215 0 ▶sub graphviz {
Subroutine graphviz: not called CC=8 SCAR=42.8
Pod undocumented
216 0 my ($h,%opts) = @_;
217 0 ▶ $opts{nodelabel} = ":NAME:\n:ATTRIBUTES:" unless (exists($opts{nodelabel}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"nodelabel"} 0 0 0
218 0 ▶ $opts{label_node} = \&_gv_label_node unless (exists($opts{label_node}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"label_node"} 0 0 0
219  
220 0 my $g = GraphViz->new(directed => 1,
221 rankdir => 0, # top->bottom linking
222 node => {
223 shape => 'plaintext',
224 },
225 edge => {
226 dir => 'none',
227 },
228 %opts);
229 0 my ($type,$parent,$label);
230  
231 # add nodes
232 0 foreach $type ($h->types) {
233 0 ▶ if (ref($opts{label_node}) && ref($opts{label_node}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{'label_node'} and ref $opts{'label_node'} eq 'CODE') { } 0 0 0
Condition0%0/3
ref $opts{"label_node"} and ref $opts{"label_node"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $opts{"label_node"} and ref $opts{"label_node"} eq "CODE"
ref $opts{"label_node"} ref $opts{"label_node"} eq "CODE"
Truth table0%0/3
A and B
A= ref $opts{"label_node"}B= ref $opts{"label_node"} eq "CODE"
ABresult
0X0
100
111
234 0 $label = &{$opts{label_node}}($h,$type,\%opts);
235 } else {
236 0 $label = "$type";
237 }
238 0 $g->add_node($type, label => $label);
239 }
240  
241 # add edges
242 0 foreach $type ($h->types) {
243 0 foreach $parent ($h->parents($type)) {
244 0 $g->add_edge($type, $parent);
245 }
246 }
247 0 return $g;
248 }
249  
250 # label_node callback($h,$t,$opts)
251 0 ▶sub _gv_label_node {
Subroutine _gv_label_node: not called CC=3 SCAR=24.8
252 0 my ($h,$type,$opts) = @_;
253 0 my ($label);
254 0 ▶ if ($h->can('get_appr_bytype')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->can('get_appr_bytype')) { } 0 0 0
255 # we have Approp
256 0 $label = "$type";
257 0 my $appr = $h->get_appr_bytype($type);
258 0 my ($f);
259 0 foreach $f (keys(%$appr)) {
260 0 $label .= "\n$f:$appr->{$f}";
261 }
262 } else {
263 # default labelling (hack)
264 0 my $attrs = $h->_attributes($type);
265 0 my $attrstr = join("\n", map { "$_:" . $attrs->{$_} } keys(%$attrs));
266 0 $label = $opts->{nodelabel};
267 0 $label =~ s/:NAME:/$type/;
268 0 $label =~ s/:ATTRIBUTES:/$attrstr/;
269 }
270 0 return $label;
271 }
272  
273 ###############################################################
274 # Viewing Utility
275 ###############################################################
276 7 *gv = \&viewps;
277 0 ▶sub viewps {
Subroutine viewps: not called CC=2 SCAR=17.9
Pod undocumented
278 0 my $h = shift;
279 0 my ($fh,$filename) = File::Temp::tempfile('hiXXXXXX', SUFFIX => '.ps');
280 0 $fh->print($h->graphviz->as_ps);
281 0 close($fh);
282 0 system("$Math::PartialOrder::Loader::PS_VIEWER \"$filename\" &");
283 0 ▶ if ($Math::PartialOrder::Loader::UNLINK_TMPFILES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Math::PartialOrder::Loader::UNLINK_TMPFILES) 0 0 0
284 0 push(@Math::PartialOrder::Loader::TMPFILES,$filename);
285 0 sleep(1);
286 }
287 }
288  
289 ##############################################################
290 # Binary store/retrieve
291 ###############################################################
292  
293 # $h->store($file)
294 5 ▶sub store {
Subroutine store: called CC=4 SCAR=14.2
Pod undocumented
295 5 my ($h,$file) = @_;
296  
297 5 ▶ my $handle = ref($file) ? $file : FileHandle->new(">$file");
Branch: 1 branch
BranchTrueFalseTotal
ref $file ? : 0 5 5
298 5 ▶ croak("open failed for file `$file': $!") unless (defined($handle));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $handle 0 5 5
299  
300 5 my $storeme = $h->_store;
301 5 Storable::store_fd($storeme->{Head}, $handle); # store headers first
302 5 delete($storeme->{Head}); # ... and only once
303 5 Storable::store_fd($storeme, $handle);
304  
305 5 ▶ $handle->close() unless (ref($file));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file 5 0 5
306 5 return $h;
307 }
308 # $h->nstore($file)
309 0 ▶sub nstore {
Subroutine nstore: not called CC=4 SCAR=30.0
Pod undocumented
310 0 my ($h,$file) = @_;
311  
312 0 ▶ my $handle = ref($file) ? $file : FileHandle->new(">$file");
Branch: 1 branch
BranchTrueFalseTotal
ref $file ? : 0 0 0
313 0 ▶ croak("open failed for file `$file': $!") unless (defined($handle));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $handle 0 0 0
314  
315 0 my $storeme = $h->_store;
316 0 Storable::nstore_fd($storeme->{Head}, $handle); # store headers first
317 0 delete($storeme->{Head}); # ... and only once
318 0 Storable::nstore_fd($storeme, $handle);
319  
320 0 ▶ $handle->close() unless (ref($file));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file 0 0 0
321 0 return $h;
322 }
323 # $h->retrieve($file)
324 5 ▶sub retrieve {
Subroutine retrieve: called CC=6 SCAR=20.7
Pod undocumented
325 5 my $h = shift;
326 5 my $file = shift;
327  
328 5 ▶ my $handle = ref($file) ? $file : FileHandle->new("<$file");
Branch: 1 branch
BranchTrueFalseTotal
ref $file ? : 0 5 5
329 5 ▶ croak("open failed for file `$file': $!") unless (defined($handle));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $handle 0 5 5
330  
331 # get and check headers
332 5 my $head = Storable::retrieve_fd($handle);
333 5 ▶ unless (defined($h->_retrieve_head($head))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $h->_retrieve_head($head)) 0 5 5
334 0 ▶ carp("Error: retrieve($file) failed for hierarchy of class `", ref($h) || $h, "'");
Condition0%0/3
ref $h || $h
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $h || $h
ref $h $h
Truth table0%0/3
A || B
A= ref $hB= $h
ABresult
1X1
011
000
335 0 return $h;
336 }
337  
338 # do the retrieval
339 5 my $retr = Storable::retrieve_fd($handle);
340 5 $retr->{Head} = $head;
341 5 my $rv = $h->_retrieve($retr);
342  
343 # and clean things up
344 5 ▶ $handle->close() unless (ref($file));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file 5 0 5
345 5 return $rv;
346 }
347  
348  
349 ##############################################################
350 # In-Memory Store/Retrieve
351 ###############################################################
352  
353 # $frozen = $h->freeze();
354 0 ▶sub freeze { return Storable::freeze($_[0]->_store); }
Subroutine freeze: not called CC=1 SCAR=6.9
Pod undocumented
355  
356 # $h->thaw($frozen);
357 0 ▶sub thaw { return $_[0]->_retrieve(Storable::thaw($_[1])); }
Subroutine thaw: not called CC=1 SCAR=6.9
Pod undocumented
358  
359  
360  
361 #--------------------------------------------------------------
362 # Storage: $h->_store() => $Ref_To_Store
363 # + hooks:
364 # _store_before(\%storeme),
365 # _store_type(\@typerec,\%storeme)
366 # _store_after(\%storeme)
367  
368 #--------------------------------------------------------------
369 # $headers = $h->_store_head(),
370 # $headers = $class->_store_head()
371 5 ▶sub _store_head {
Subroutine _store_head: called CC=2 SCAR=8.1
372 5 my $h = shift;
373 return
374 {
375 5 ▶ Class => ref($h) || $h,
Condition33%1/3
ref $h || $h
l!l&&r!l&&!r
500
MC/DC0%0/2
ref $h || $h
ref $h $h
Truth table33%1/3
A || B
A= ref $hB= $h
ABresult
1X1
011
000
376 Cversion => $h->VERSION,
377 Ccompat => $h->_get_bin_compat,
378 Hstring => "$h",
379 Lversion => $Math::PartialOrder::Loader::VERSION,
380 Lcompat => $Math::PartialOrder::Loader::BIN_COMPAT
381 };
382 }
383  
384 #--------------------------------------------------------------
385 5 ▶sub _store {
Subroutine _store: called CC=8 SCAR=21.1
386 5 my $h = shift;
387 5 ▶ my $class = ref($h) || $h;
Condition33%1/3
ref $h || $h
l!l&&r!l&&!r
500
MC/DC0%0/2
ref $h || $h
ref $h $h
Truth table33%1/3
A || B
A= ref $hB= $h
ABresult
1X1
011
000
388 5 my $head = $h->_store_head();
389 5 my $refs = { "$h" => $h->_hattributes };
390 5 my $trs = [];
391 5 my $storeme = { Head => $head,
392 Refs => $refs,
393 Types => $trs };
394  
395 # preliminary storage-hook
396 5 ▶ $h->_store_before($storeme) if ($h->can('_store_before'));
Branch: 1 branch
BranchTrueFalseTotal
if $h->can("_store_before") 2 3 5
397  
398 5 my ($tr,$attrs);
399 5 foreach ($h->types) {
400 # update nested refs
401 40 $refs->{$_} = $_;
402 40 $attrs = $h->_attributes($_);
403 40 ▶ $refs->{$attrs} = $attrs if (defined($attrs));
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs 0 40 40
404  
405 # create the type-record
406 $tr = [
407 "$_", # name
408 40 ▶ [ map { "$_" } $h->parents($_) ], # parents
Branch: 1 branch
BranchTrueFalseTotal
defined $attrs ? : 0 40 40
409 defined($attrs)
410 ? "$attrs" # attrs | undef
411 : undef
412 ];
413  
414 # type-storage hook
415 40 ▶ $h->_store_type($tr,$storeme) if ($h->can('_store_type'));
Branch: 1 branch
BranchTrueFalseTotal
if $h->can("_store_type") 16 24 40
416  
417 40 push(@{$trs}, $tr);
418 }
419  
420 # post-processing hook
421 5 ▶ $h->_store_after($storeme) if ($h->can('_store_after'));
Branch: 1 branch
BranchTrueFalseTotal
if $h->can("_store_after") 0 5 5
422  
423 5 return $storeme;
424 }
425  
426 #--------------------------------------------------------------
427 # + \%tostore|\%retrieved format:
428 # { Head => \%Headers,
429 # Refs => \%RefsByString,
430 # Types => \@TypeRecs, ... }
431 #
432 # + \%Headers format:
433 # { Class => $ClassName,
434 # Cversion => $ClassVersion,
435 # Hstring => "$HierarchyAsString",
436 # Lversion => $LoaderVersion,
437 # Lcompat => $MinLoaderVersion }
438 #
439 # + \%RefsByString format: { "$OldStringVal" => $Reference }
440 # + \@TypeRecs format: [ \@TypeRec1, ..., \@TypeRecN ]
441 # + \@TypeRec format: [ "$TypeName", \@ParentsNames, "$AttrsName" ]
442 #--------------------------------------------------------------
443  
444  
445 #--------------------------------------------------------------
446 # Compatibility
447 #--------------------------------------------------------------
448 0 ▶sub _is_bin_compat { return undef; } # just a dummy
Subroutine _is_bin_compat: not called CC=1 SCAR=6.9
449 3 ▶sub _get_bin_compat { return {}; } # just a dummy
Subroutine _get_bin_compat: called CC=1 SCAR=0.0
450  
451  
452 #--------------------------------------------------------------
453 # Retrieval: $h->_retrieve(\%retrieved) => $h
454 # + hooks:
455 # _retrieve_before(\%storeme),
456 # _retrieve_type_before(\@typerec,\%storeme)
457 # _retrieve_type(\@typerec,\%storeme)
458 # _retrieve_type_after(\@typerec,\%storeme)
459 # _retrieve_after(\%storeme)
460 #
461 5 ▶sub _retrieve {
Subroutine _retrieve: called CC=14 SCAR=33.7
462 5 my ($h,$retr) = @_;
463 5 my ($compat);
464  
465 # can we do this?
466 5 ▶ unless (defined($compat = $h->_retrieve_head($retr->{Head}))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($compat = $h->_retrieve_head($retr->{"Head"}))) 0 5 5
467 0 ▶ carp("Error: _retrieve() failed for hierarchy of class `", ref($h) || $h, "'");
Condition0%0/3
ref $h || $h
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $h || $h
ref $h $h
Truth table0%0/3
A || B
A= ref $hB= $h
ABresult
1X1
011
000
468 0 return $h;
469 }
470  
471 # get the hierarchy attributes...
472 5 $h->_hattributes($retr->{Refs}{$retr->{Head}{Hstring}});
473  
474 # preliminary retrieval
475 5 ▶ $h->_retrieve_before($retr) if ($compat && $h->can('_retrieve_before'));
Branch: 1 branch
BranchTrueFalseTotal
if $compat and $h->can("_retrieve_before") 0 5 5
Condition33%1/3
$compat and $h->can("_retrieve_before")
!ll&&!rl&&r
050
MC/DC0%0/2
$compat and $h->can("_retrieve_before")
$compat $h->can("_retrieve_before")
Truth table33%1/3
A and B
A= $compatB= $h->can("_retrieve_before")
ABresult
0X0
100
111
476  
477 # get the types
478 5 my $typerecs = $retr->{Types};
479 5 my ($tr);
480 5 foreach $tr (@$typerecs) {
481 # preliminary type-retrieval
482 40 ▶ $h->_retrieve_type_before($tr,$retr) if ($compat && $h->can('_retrieve_type_before'));
Branch: 1 branch
BranchTrueFalseTotal
if $compat and $h->can("_retrieve_type_before") 0 40 40
Condition33%1/3
$compat and $h->can("_retrieve_type_before")
!ll&&!rl&&r
0400
MC/DC0%0/2
$compat and $h->can("_retrieve_type_before")
$compat $h->can("_retrieve_type_before")
Truth table33%1/3
A and B
A= $compatB= $h->can("_retrieve_type_before")
ABresult
0X0
100
111
483  
484 # get the actual type
485 40 ▶ if ($compat && $h->can('_retrieve_type')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compat and $h->can('_retrieve_type')) { } 40 0 40
Condition33%1/3
$compat and $h->can("_retrieve_type")
!ll&&!rl&&r
0040
MC/DC0%0/2
$compat and $h->can("_retrieve_type")
$compat $h->can("_retrieve_type")
Truth table33%1/3
A and B
A= $compatB= $h->can("_retrieve_type")
ABresult
0X0
100
111
486 # override
487 40 $h->_retrieve_type($tr,$retr);
488 } else {
489 # defaults
490 0 _retrieve_type($h,$tr,$retr);
491 }
492  
493 # postprocessing
494 40 ▶ $h->_retrieve_type_after($tr,$retr) if ($compat && $h->can('_retrieve_type_after'));
Branch: 1 branch
BranchTrueFalseTotal
if $compat and $h->can("_retrieve_type_after") 0 40 40
Condition33%1/3
$compat and $h->can("_retrieve_type_after")
!ll&&!rl&&r
0400
MC/DC0%0/2
$compat and $h->can("_retrieve_type_after")
$compat $h->can("_retrieve_type_after")
Truth table33%1/3
A and B
A= $compatB= $h->can("_retrieve_type_after")
ABresult
0X0
100
111
495 }
496  
497 5 ▶ $h->_retrieve_after($retr) if ($compat && $h->can('_retrieve_after'));
Branch: 1 branch
BranchTrueFalseTotal
if $compat and $h->can("_retrieve_after") 2 3 5
Condition66%2/3
$compat and $h->can("_retrieve_after")
!ll&&!rl&&r
032
MC/DC50%1/2
$compat and $h->can("_retrieve_after")
$compat $h->can("_retrieve_after")
Truth table66%2/3
A and B
A= $compatB= $h->can("_retrieve_after")
ABresult
0X0
100
111
498 5 return $h;
499 }
500  
501  
502 # $h->_retrieve_type($rec,$retr)
503 # $retr = { Head => \@Hdrs, Types => \@TypeRecs, Refs => \%Refs }
504 # $tr = [ $Name, \@Parents, $AttrsName ]
505 24 ▶sub _retrieve_type {
Subroutine _retrieve_type: called CC=2 SCAR=7.0
506 $_[0]->add($_[2]->{Refs}{$_[1]->[$_tr_name]},
507 24 @{$_[2]->{Refs}}{@{$_[1]->[$_tr_parents]}});
508 $_[0]->_attributes
509 ($_[2]->{Refs}{$_[1]->[$_tr_name]},
510 24 ▶ $_[2]->{Refs}{$_[1]->[$_tr_attrs]}) if (defined($_[1]->[$_tr_attrs]));
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1][$Math::PartialOrder::Loader::_tr_attrs] 0 24 24
511 }
512  
513 # $bool_or_undef = $h->_retrieve_head($head)
514 10 ▶sub _retrieve_head {
Subroutine _retrieve_head: called CC=20 SCAR=50.7
515 10 my ($h,$head) = @_;
516 10 my ($class,$compat);
517  
518 # check reftype & existence
519 10 ▶ unless (ref($head) && ref($head) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $head and ref $head eq "HASH") 0 10 10
Condition33%1/3
ref $head and ref $head eq "HASH"
!ll&&!rl&&r
0010
MC/DC0%0/2
ref $head and ref $head eq "HASH"
ref $head ref $head eq "HASH"
Truth table33%1/3
A and B
A= ref $headB= ref $head eq "HASH"
ABresult
0X0
100
111
520 0 carp("Warning: non-hashref '$head' cannot be header");
521 0 return undef;
522 }
523  
524 # does the stored hierarchy have a version?
525 10 ▶ if (!defined($head->{Lversion})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $head->{"Lversion"}) 0 10 10
526 0 carp("Warning: stored hierarchy has no Lversion");
527 0 return undef;
528 }
529  
530 # does the stored hierarchy have a version?
531 10 ▶ if (!defined($head->{Lcompat})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $head->{"Lcompat"}) 0 10 10
532 0 carp("Warning: stored hierarchy has no Lcompat");
533 0 return undef;
534 }
535  
536 # does the stored hierarchy have a hierarchy-string?
537 10 ▶ if (!defined($head->{Hstring})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $head->{"Hstring"}) 0 10 10
538 0 carp("Warning: stored hierarchy has no Hstring");
539 0 return undef;
540 }
541  
542 # is the storage-routine too old?
543 10 ▶ if ($head->{Lversion} < $Math::PartialOrder::Loader::BIN_COMPAT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($head->{"Lversion"} < $Math::PartialOrder::Loader::BIN_COMPAT) 0 10 10
544 0 carp("Warning: obsolete Math::PartialOrder::Loader stored hierarchy\n",
545 " > stored version = $head->{Lversion}\n",
546 " > required version >= $Math::PartialOrder::Loader::BIN_COMPAT\n",
547 " >");
548 0 return undef;
549 }
550  
551 # is this load-routine too old?
552 10 ▶ if ($Math::PartialOrder::Loader::VERSION < $head->{Lcompat}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Math::PartialOrder::Loader::VERSION < $head->{"Lcompat"}) 0 10 10
553 0 carp("Warning: obsolete Math::PartialOrder::Loader retrieval routine\n",
554 " > this version = $Math::PartialOrder::Loader::VERSION\n",
555 " > required version >= $head->{Lcompat}\n",
556 " >");
557 0 return undef;
558 }
559  
560 # do we have class?
561 10 ▶ if (!defined($head->{Class})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $head->{"Class"}) 0 10 10
562 0 carp("Warning: stored hierarchy has no Class!");
563 0 return 0;
564 }
565  
566 # do we have class-version?
567 10 ▶ if (!defined($head->{Cversion})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $head->{"Cversion"}) 0 10 10
568 0 carp("Warning: stored hierarchy has no Cversion");
569 0 return 0;
570 }
571  
572 # is the stored hierarchy the same class & version as the caller?
573 10 ▶ if (defined($class = (ref($h)||$h))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($class = ref $h || $h) and $head->{"Class"} eq $class and $head->{"Cversion"} == $h->VERSION) 10 0 10
Condition33%3/9
defined($class = ref $h || $h) and $head->{"Class"} eq $class and $head->{"Cversion"} == $h->VERSION
!ll&&!rl&&r
0010
defined($class = ref $h || $h) and $head->{"Class"} eq $class
!ll&&!rl&&r
0010
ref $h || $h
l!l&&r!l&&!r
1000
MC/DC0%0/4
defined($class = ref $h || $h) and $head->{"Class"} eq $class and $head->{"Cversion"} == $h->VERSION
ref $h $h $head->{"Class"} eq $class $head->{"Cversion"} == $h->VERSION
Truth table14%1/7
A || B and C and D
A= ref $hB= $hC= $head->{"Class"} eq $classD= $head->{"Cversion"} == $h->VERSION
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
574 && $head->{Class} eq $class
575 && $head->{Cversion} == $h->VERSION)
576 {
577 10 return 1; # whew.
578 }
579  
580 # does the stored thingy have compatibility hash?
581 0 ▶ if (!defined($head->{Ccompat})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $head->{"Ccompat"}) 0 0 0
582 0 carp("Warning: stored hierarchy has no Ccompat");
583 0 return 0;
584 }
585  
586 # the stored hierarchy knows something about us, too...
587 0 ▶ if (defined($head->{Ccompat}{$class})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $head->{"Ccompat"}{$class}) 0 0 0
588 0 ▶ if ($head->{Ccompat}{$class} > $h->VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($head->{"Ccompat"}{$class} > $h->VERSION) 0 0 0
589 0 carp("Warning: binary-incompatible hierarchy package detected\n",
590 " > this class = `$class'\n",
591 " > stored class = `$head->{Class}'\n",
592 " > this version = ", $class->VERSION, "\n",
593 " > required version >= $head->{Ccompat}{$class}\n",
594 " > (maybe it's time to update?)");
595 0 return 0;
596 }
597 }
598  
599 # finally, lookup in the retrieving hierarchy
600 0 ▶ if (defined($compat = $h->_get_bin_compat)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($compat = $h->_get_bin_compat) and defined $$compat{$head->{"Class"}}) 0 0 0
Condition0%0/3
defined($compat = $h->_get_bin_compat) and defined $$compat{$head->{"Class"}}
!ll&&!rl&&r
000
MC/DC0%0/2
defined($compat = $h->_get_bin_compat) and defined $$compat{$head->{"Class"}}
defined($compat = $h->_get_bin_compat) defined $$compat{$head->{"Class"}}
Truth table0%0/3
A and B
A= defined($compat = $h->_get_bin_compat)B= defined $$compat{$head->{"Class"}}
ABresult
0X0
100
111
601 && defined($compat->{$head->{Class}})) {
602 0 ▶ if ($compat->{$head->{Class}} > $head->{Cversion})
Branch: 1 branch
BranchTrueFalseTotal
if ($$compat{$head->{"Class"}} > $head->{"Cversion"}) 0 0 0
603 {
604 # just issue a warning
605 0 carp("Warning: binary-incompatible stored hierarchy detected\n",
606 " > stored class = `$head->{Class}'\n",
607 " > this class = `$class'\n",
608 " > stored version = $head->{Cversion}\n",
609 " > required version >= $compat->{$head->{Class}}\n",
610 " > (maybe it's time to recompile?)");
611 0 return 0;
612 }
613 0 return 1;
614 }
615  
616 # use the defaults
617 0 return 0;
618 }
619  
620  
621 7 ▶END {
Subroutine END: called CC=2 SCAR=7.6
622 7 ▶ unlink(@Math::PartialOrder::Loader::TMPFILES) if
Branch: 1 branch
BranchTrueFalseTotal
if $Math::PartialOrder::Loader::UNLINK_TMPFILES 7 0 7
623 ($Math::PartialOrder::Loader::UNLINK_TMPFILES);
624 }
625  
626 7 1;
627 __END__
« PartialOrder/CMasked.pm ↑ Summary PartialOrder/Base.pm »