CAB/Unify.pm

filter: Statementstmt 30.6% 23 / 75 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 35.7% 5 / 14 Podpod 75.0% 3 / 4 totaltotal 15.6% 31 / 198 CC 40 SCAR 61.9
CC
40
Cov
14%
_unify_guts
870.0
unifyClone_
72.0
unifyClone
20.0
CRAP 488.9
« CAB/Format/TCF.pm ↑ Summary CAB/Analyzer/Automaton.pm »
1 #-*- Mode: CPerl -*-
2  
3 ## File: DTA::CAB::Unify.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description:
6 ## + Unification utiltities (copied from Taxi::Mysql::Unify)
7 ##======================================================================
8  
9 package DTA::CAB::Unify;
10 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use UNIVERSAL;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 *isa = \&UNIVERSAL::isa;
17 1 *can = \&UNIVERSAL::can;
18 };
19  
20 ##======================================================================
21 ## Globals
22  
23 ## $TOP
24 ## scalar used by _unify() and friends for incompatible unifications
25 1 our $TOP = '__TOP__';
26  
27 1 our @ISA = qw(Exporter);
28  
29 ## @EXPORT
30 ## By default, unify(), unifyClobber(), unifyClone() are exported
31 1 our @EXPORT = qw(unify unifyClobber unifyClone);
32  
33 ## %EXPORT_TAGS
34 ## Known tags: :default (see @EXPORT), :misc (_unify* subs), :all (everything)
35 1 our %EXPORT_TAGS =
36 (
37 'default' =>['unify','unifyClobber','unifyClone'],
38 'misc' =>['_unify', '_unifyClobber', '_unify_guts', '_unify1_top', '_unify1_clobber'],
39 );
40 1 $EXPORT_TAGS{all} = [map { @$_ } values(%EXPORT_TAGS)];
41 1 our @EXPORT_OK = @{$EXPORT_TAGS{all}};
42  
43  
44 ##======================================================================
45 ## API: Unification
46  
47 ## $xnew = unifyClone($x)
48 ## + wrapper for Storable::dclone()
49 0 ▶sub unifyClone {
Subroutine unifyClone: not called CC=4 SCAR=30.0
Pod documented
50 0 ▶ if (!defined($_[0])) { return undef; }
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $_[0]) { } 0 0 0
elsif (not ref $_[0]) { } 0 0 0
elsif (can($_[0], 'clone')) { } 0 0 0
51 0 elsif (!ref($_[0])) { return "$_[0]" }
52 0 elsif (can($_[0],'clone')) { return $_[0]->clone; }
53 0 else { Storable::dclone($_[0]); }
54 }
55  
56 ## $xnew = unifyClone_($x)
57 ## + re-implemented to avoid Storable::dclone()
58 ## + seems to cause infinite load-loop and memory-gobble in cab server: why?
59 0 ▶sub unifyClone_ {
Subroutine unifyClone_: not called CC=8 SCAR=42.8
Pod undocumented
60 0 ▶ if (!defined($_[0])) { return undef; }
Branch: 5 branches
BranchTrueFalseTotal
if (not defined $_[0]) { } 0 0 0
elsif (not ref $_[0]) { } 0 0 0
elsif (DTA::CAB::Unify::isa($_[0], 'REGEXP')) { } 0 0 0
elsif (DTA::CAB::Unify::isa($_[0], 'HASH')) { } 0 0 0
elsif (DTA::CAB::Unify::isa($_[0], 'ARRAY')) { } 0 0 0
61 0 elsif (!ref($_[0])) { return "$_[0]" }
62 0 elsif (isa($_[0],'REGEXP')) { return qr($_[0]); }
63 0 ▶ elsif (isa($_[0],'HASH')) { my $tmp={ map {unifyClone($_)} %{$_[0]} }; return ref($_[0]) eq 'HASH' ? $tmp : bless($tmp,ref($_[0])); }
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
64 0 ▶ elsif (isa($_[0],'ARRAY')) { my $tmp=[ map {unifyClone($_)} @{$_[0]} ]; return ref($_[0]) eq 'ARRAY' ? $tmp : bless($tmp,ref($_[0])); }
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
65 0 else { return $_[0]; } ##-- cowardly refuse to clone GLOBs, CODE-refs, etc
66 }
67  
68  
69 ## $xy = unify($x,$y, $OUTPUT_TOP)
70 0 ▶sub unify { return _unify_guts(unifyClone($_[0]),unifyClone($_[1]),\&_unify1_top, @_[2..$#_]); }
Subroutine unify: not called CC=1 SCAR=6.9
Pod documented
71  
72 ## $xy = unifyClobber($x,$y, $OUTPUT_TOP)
73 ## + clobbers old values of $x with new values from $y if unification would produce $TOP
74 0 ▶sub unifyClobber { return _unify_guts(unifyClone($_[0]),unifyClone($_[1]),\&_unify1_clobber, @_[2..$#_]); }
Subroutine unifyClobber: not called CC=1 SCAR=6.9
Pod documented
75  
76 ## $xy = _unify($x,$y, $OUTPUT_TOP)
77 ## + destructively alters $x, adopts literal references from $y where possible
78 ## + does *NOT* clobber defined values in $x with undef values in $y!
79 ## - to clobber defined $x values with undef in $y, set $y values to $TOP and pass $OUTPUT_TOP=undef
80 0 ▶sub _unify { return _unify_guts($_[0],$_[1],\&_unify1_top, @_[2..$#_]); }
Subroutine _unify: not called CC=1 SCAR=6.9
81  
82 ## $xy = _unifyClobber($x,$y)
83 ## + destructively alters $x, adopts literal references from $y where possible
84 0 ▶sub _unifyClobber { return _unify_guts($_[0],$_[1],\&_unify1_clobber, @_[2..$#_]); }
Subroutine _unifyClobber: not called CC=1 SCAR=6.9
85  
86 ## $x_altered = _unify_guts($x,$y,\&unify1_sub)
87 ## + destructively alters $x, adopts literal references from $y where possible
88 0 ▶sub _unify_guts {
Subroutine _unify_guts: not called CC=29 SCAR=67.7
89 0 my ($x0,$y0,$uscalar,$topout) = @_;
90 0 ▶ $topout = $TOP if (!exists($_[3]));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_[3] 0 0 0
91 0 ▶ $uscalar = \&_unify1_top if (!defined($uscalar));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uscalar 0 0 0
92 0 my @eqr = (\$x0,\$y0);
93 0 my ($x,$y);
94 0 ▶ while (@eqr) {
Branch: 1 branch
BranchTrueFalseTotal
if (@eqr) 0 0 0
95 0 ($x,$y) = splice(@eqr,0,2);
96 ##
97 0 ▶ if (defined($$x) && $$x eq $TOP) { $$x=$topout; } ##-- Case: (TOP,$y) -> $OUTPUT_TOP
Branch: 10 branches
BranchTrueFalseTotal
if (defined $$x and $$x eq $DTA::CAB::Unify::TOP) { } 0 0 0
elsif (defined $$y and $$y eq $DTA::CAB::Unify::TOP) { } 0 0 0
elsif (not defined $$x) { } 0 0 0
elsif (not defined $$y) { } 0 0 0
elsif (DTA::CAB::Unify::isa($$x, 'HASH') and DTA::CAB::Unify::isa($$y, 'HASH')) { } 0 0 0
elsif (DTA::CAB::Unify::isa($$x, 'ARRAY') and DTA::CAB::Unify::isa($$x, 'ARRAY')) { } 0 0 0
elsif (not ref $$x || ref $$y) { } 0 0 0
elsif (UNIVERSAL::isa($$x, 'Regexp') or UNIVERSAL::isa($$y, 'Regexp')) { } 0 0 0
elsif (DTA::CAB::Unify::isa($$x, 'REF') and DTA::CAB::Unify::isa($$y, 'REF')) { } 0 0 0
elsif (DTA::CAB::Unify::isa($$x, 'SCALAR') and DTA::CAB::Unify::isa($$y, 'SCALAR')) { } 0 0 0
Condition0%0/3
defined $$x and $$x eq $DTA::CAB::Unify::TOP
!ll&&!rl&&r
000
MC/DC0%0/2
defined $$x and $$x eq $DTA::CAB::Unify::TOP
defined $$x $$x eq $DTA::CAB::Unify::TOP
Truth table0%0/3
A and B
A= defined $$xB= $$x eq $DTA::CAB::Unify::TOP
ABresult
0X0
100
111
98 0 ▶ elsif (defined($$y) && $$y eq $TOP) { $$x=$topout; } ##-- Case: (TOP,$y) -> $OUTPUT_TOP
Condition0%0/3
defined $$y and $$y eq $DTA::CAB::Unify::TOP
!ll&&!rl&&r
000
MC/DC0%0/2
defined $$y and $$y eq $DTA::CAB::Unify::TOP
defined $$y $$y eq $DTA::CAB::Unify::TOP
Truth table0%0/3
A and B
A= defined $$yB= $$y eq $DTA::CAB::Unify::TOP
ABresult
0X0
100
111
99 0 elsif (!defined($$x)) { $$x=$$y; } ##-- Case: (undef,$y) -> $y
100 0 elsif (!defined($$y)) { next; } ##-- Case: ($x,undef) -> $x
101 ▶ elsif (isa($$x,'HASH') && isa($$y,'HASH')) { ##-- Case: (\%x,\%y)
Condition0%0/3
DTA::CAB::Unify::isa($$x, "HASH") and DTA::CAB::Unify::isa($$y, "HASH")
!ll&&!rl&&r
000
MC/DC0%0/2
DTA::CAB::Unify::isa($$x, "HASH") and DTA::CAB::Unify::isa($$y, "HASH")
DTA::CAB::Unify::isa($$x, "HASH") DTA::CAB::Unify::isa($$y, "HASH")
Truth table0%0/3
A and B
A= DTA::CAB::Unify::isa($$x, "HASH")B= DTA::CAB::Unify::isa($$y, "HASH")
ABresult
0X0
100
111
102 0 push(@eqr, map { (\$$x->{$_},\$$y->{$_}) } keys(%$$y));
103 0 ▶ bless($$x,ref($$y)) if (ref($$y) ne 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $$y ne "HASH" 0 0 0
104 }
105 ▶ elsif (isa($$x,'ARRAY') && isa($$x,'ARRAY')) { ##-- Case: (\@x,\@y)
Condition0%0/3
DTA::CAB::Unify::isa($$x, "ARRAY") and DTA::CAB::Unify::isa($$x, "ARRAY")
!ll&&!rl&&r
000
MC/DC0%0/2
DTA::CAB::Unify::isa($$x, "ARRAY") and DTA::CAB::Unify::isa($$x, "ARRAY")
DTA::CAB::Unify::isa($$x, "ARRAY") DTA::CAB::Unify::isa($$x, "ARRAY")
Truth table0%0/3
A and B
A= DTA::CAB::Unify::isa($$x, "ARRAY")B= DTA::CAB::Unify::isa($$x, "ARRAY")
ABresult
0X0
100
111
106 push(@eqr,
107 0 map { (\$$x->[$_],\$$y->[$_]) }
108 0 ▶ grep { exists($$x->[$_]) || exists($$y->[$_]) }
Branch: 1 branch
BranchTrueFalseTotal
$$x->$#* > $$y->$#* ? : 0 0 0
Condition0%0/3
exists $$x->[$_] || exists $$y->[$_]
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $$x->[$_] || exists $$y->[$_]
exists $$x->[$_] exists $$y->[$_]
Truth table0%0/3
A || B
A= exists $$x->[$_]B= exists $$y->[$_]
ABresult
1X1
011
000
109 (0..($#$$x > $#$$y ? $#$$x : $#$$y))
110 );
111 0 ▶ bless($$x,ref($$y)) if (ref($$y) ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $$y ne "ARRAY" 0 0 0
112 }
113 ▶ elsif (!ref($$x) && !ref($$y)) { ##-- Case: ($x,$y)
Condition0%0/3
ref $$x || ref $$y
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $$x || ref $$y
ref $$x ref $$y
Truth table0%0/3
A || B
A= ref $$xB= ref $$y
ABresult
1X1
011
000
114 0 $$x = $uscalar->($$x,$$y);
115 }
116 ▶ elsif (UNIVERSAL::isa($$x,'Regexp') || UNIVERSAL::isa($$y,'Regexp')) { ##-- Case: (qr//,qr//)
Condition0%0/3
UNIVERSAL::isa($$x, "Regexp") or UNIVERSAL::isa($$y, "Regexp")
l!l&&r!l&&!r
000
MC/DC0%0/2
UNIVERSAL::isa($$x, "Regexp") or UNIVERSAL::isa($$y, "Regexp")
UNIVERSAL::isa($$x, "Regexp") UNIVERSAL::isa($$y, "Regexp")
Truth table0%0/3
A or B
A= UNIVERSAL::isa($$x, "Regexp")B= UNIVERSAL::isa($$y, "Regexp")
ABresult
1X1
011
000
117 0 $$x = $uscalar->($$x,$$y);
118 0 ▶ bless($$x,ref($$y)) if (ref($$y) ne 'Regexp');
Branch: 1 branch
BranchTrueFalseTotal
if ref $$y ne "Regexp" 0 0 0
119 }
120 ▶ elsif (isa($$x,'REF') && isa($$y,'REF')) { ##-- Case: (\\?x,\\?y)
Condition0%0/3
DTA::CAB::Unify::isa($$x, "REF") and DTA::CAB::Unify::isa($$y, "REF")
!ll&&!rl&&r
000
MC/DC0%0/2
DTA::CAB::Unify::isa($$x, "REF") and DTA::CAB::Unify::isa($$y, "REF")
DTA::CAB::Unify::isa($$x, "REF") DTA::CAB::Unify::isa($$y, "REF")
Truth table0%0/3
A and B
A= DTA::CAB::Unify::isa($$x, "REF")B= DTA::CAB::Unify::isa($$y, "REF")
ABresult
0X0
100
111
121 0 push(@eqr, $$x,$$y);
122 0 ▶ bless($$x,ref($$y)) if (ref($$y) ne 'REF');
Branch: 1 branch
BranchTrueFalseTotal
if ref $$y ne "REF" 0 0 0
123 }
124 ▶ elsif (isa($$x,'SCALAR') && isa($$y,'SCALAR')) { ##-- Case: (\$x,\$y)
Condition0%0/3
DTA::CAB::Unify::isa($$x, "SCALAR") and DTA::CAB::Unify::isa($$y, "SCALAR")
!ll&&!rl&&r
000
MC/DC0%0/2
DTA::CAB::Unify::isa($$x, "SCALAR") and DTA::CAB::Unify::isa($$y, "SCALAR")
DTA::CAB::Unify::isa($$x, "SCALAR") DTA::CAB::Unify::isa($$y, "SCALAR")
Truth table0%0/3
A and B
A= DTA::CAB::Unify::isa($$x, "SCALAR")B= DTA::CAB::Unify::isa($$y, "SCALAR")
ABresult
0X0
100
111
125 0 push(@eqr, $$x,$$y);
126 0 ▶ bless($$x,ref($$y)) if (ref($$y) ne 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
if ref $$y ne "SCALAR" 0 0 0
127 }
128 else { ##-- Case: ?
129 #carp( __PACKAGE__ . "::_unify_guts(): don't know how to unify (x=$x, y=$y): treating as scalars");
130 0 $$x = $uscalar->($$x,$$y); ##-- default: treat as scalars
131 }
132 }
133 0 return $x0;
134 }
135  
136 ## $xval = _unify1_top($x,$y)
137 ## + called for simple scalars
138 0 ▶sub _unify1_top { return $_[0] eq $_[1] ? $_[0] : $TOP; }
Subroutine _unify1_top: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq $_[1] ? : 0 0 0
139  
140 ## $xval = _unify1_clobber($x,$y)
141 ## + called for simple scalars
142 ## + maps TOP to undef
143 0 ▶sub _unify1_clobber { return $_[1]; }
Subroutine _unify1_clobber: not called CC=1 SCAR=6.9
144  
145 1 1;
146  
147 __END__
« CAB/Format/TCF.pm ↑ Summary CAB/Analyzer/Automaton.pm »