Abstract/Peeler.pm

filter: Statementstmt 81.7% 224 / 274 Branchbran 70.5% 127 / 180 Conditioncond 44.1% 68 / 154 MC/DCmcdc 18.1% 14 / 77 Subroutinesub 88.2% 30 / 34 Podpod 28.5% 6 / 21 totaltotal 63.3% 469 / 740 CC 148 SCAR 36.8
CC
148
Cov
69%
__ANON__
92.0
extract
42.0
new
17.1
CRAP 39.8
↑ Summary Abstract.pm »
1 package Neo4j::Cypher::Abstract::Peeler;
2 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use List::Util 1.33 qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Scalar::Util qw(looks_like_number blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # quoting logic:
9 # if config:bind = true
10 # if anon_placeholder (like ?) is in config, then return literals without
11 # quoting in array $obj->bind_values, and the placeholder in the statement
12 # if anon_placeholder is undef, then return literals quoted directly in the
13 # statement; return named parameters in $obj->parameters
14 #
15 # if config:bind false
16 # leave tokens and identifiers as-is, no bind_values or parameters
17  
18 6 our $VERSION = '0.1002';
19 6 my $SQL_ABSTRACT = 1;
20  
21 sub puke(@);
22 sub belch(@);
23  
24 6 my %config = (
25 bind => 1,
26 anon_placeholder => undef, # '?',
27 hash_op => '-and',
28 array_op => '-or',
29 list_braces => '[]',
30 ineq_op => '<>',
31 implicit_eq_op => '=',
32 quote_lit => "'",
33 esc_quote_lit => "\\",
34 parameter_sigil => qw/^([:$?])|({[^}]+}$)/,
35 quote_fld => undef,
36 safe_identifier => qw/[a-zA-Z_.]+/
37 );
38  
39 # for each operator type (key in %type_table), there
40 # should be a handler with the same name
41  
42 6 my %type_table = (
43 infix_binary => [qw{ - / % =~ = <> < > <= >=
44 -contains -starts_with -ends_with}],
45 infix_listarg => [qw{ -in } ],
46 infix_distributable => [qw{ + * -and -or }],
47 prefix => [qw{ -not }],
48 postfix => [qw{ -is_null -is_not_null }],
49 function => [qw{
50 ()
51 -abs -ceil -floor -rand -round -sign -degrees
52 -e -exp -log -log10 -sqrt -acos -asin -atan -atan2
53 -cos -cot -haversin -pi -radians -sin -tan
54 -exists -toInt
55 -left -lower -ltrim -replace -reverse -right
56 -rtrim -split -substring -toString -trim -upper
57 -length -size -type -id -coalesce -head -last
58 -labels -nodes -relationships -keys -tail -range
59 -collect -count -max -min -percentileCont
60 -percentileDisc -stDev -stDevP -sum
61 -shortestPath -allShortestPaths
62 }],
63 predicate => [qw{ -all -any -none -single -filter}],
64 extract => [qw{ -extract }],
65 reduce => [qw{ -reduce }],
66 list => [qw( -list )], # returns args in list format
67 bind => [qw( -bind )], # handles parameters and literal quoting
68 thing => [qw( -thing )], # the literal thing itself
69 );
70  
71 6 my %dispatch;
72 6 foreach my $type (keys %type_table) {
73 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
74 72 my @ops = @{$type_table{$type}};
75 72 @dispatch{@ops} = ( *${type}{CODE} ) x @ops;
76 }
77  
78 36 ▶sub new {
Subroutine new: called CC=10 SCAR=28.4
Pod undocumented
79 36 my $class = shift;
80 36 my %args = @_;
81 36 ▶ if ($args{dispatch} and !(ref $args{dispatch} eq 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"dispatch"} and not ref $args{"dispatch"} eq "HASH") 0 36 36
Condition33%1/3
$args{"dispatch"} and not ref $args{"dispatch"} eq "HASH"
!ll&&!rl&&r
3600
MC/DC0%0/2
$args{"dispatch"} and not ref $args{"dispatch"} eq "HASH"
$args{"dispatch"} not ref $args{"dispatch"} eq "HASH"
Truth table33%1/3
A and not(B)
A= $args{"dispatch"}B= not ref $args{"dispatch"} eq "HASH"
ABresult
0X0
100
111
82 0 puke "dispatch must be hashref mapping operators to coderefs (or absent)"
83 }
84 36 ▶ if ($args{config} and !(ref $args{config} eq 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"config"} and not ref $args{"config"} eq "HASH") 0 36 36
Condition33%1/3
$args{"config"} and not ref $args{"config"} eq "HASH"
!ll&&!rl&&r
3600
MC/DC0%0/2
$args{"config"} and not ref $args{"config"} eq "HASH"
$args{"config"} not ref $args{"config"} eq "HASH"
Truth table33%1/3
A and not(B)
A= $args{"config"}B= not ref $args{"config"} eq "HASH"
ABresult
0X0
100
111
85 0 puke "config must be hashref defining peeler options"
86 }
87 my $self = {
88 dispatch => $args{dispatch} || \%dispatch,
89 36 ▶ config => $args{config} || \%config
Condition50%2/4
$args{'dispatch'} || \%dispatch
l!l
036
$args{'config'} || \%config
l!l
036
MC/DC0%0/1
$args{'dispatch'} || \%dispatch
$args{'dispatch'}
MC/DC0%0/1
$args{'config'} || \%config
$args{'config'}
Truth table50%1/2
A
A= $args{'dispatch'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'config'}
Aresult
11
00
90 };
91 # update config elts according to constructor args
92 36 ▶ if (length scalar keys %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (length scalar keys %args) 36 0 36
93 36 for (keys %config) {
94 432 ▶ defined $args{$_} and $self->{config}{$_} = $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
defined $args{$_} and $self->{"config"}{$_} = $args{$_} 0 432 432
95 }
96 }
97 36 bless $self, $class;
98 }
99  
100 0 ▶sub belch (@) {
Subroutine belch: not called CC=1 SCAR=6.9
Pod undocumented
101 0 my($func) = (caller(1))[3];
102 0 Carp::carp "[$func] Warning: ", @_;
103 }
104  
105 0 ▶sub puke (@) {
Subroutine puke: not called CC=1 SCAR=6.9
Pod undocumented
106 0 my($func) = (caller(1))[3];
107 0 Carp::croak "[$func] Fatal: ", @_;
108 }
109  
110 32 ▶sub bind_values { $_[0]->{bind_values} ? @{$_[0]->{bind_values}} : return ; }
Subroutine bind_values: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'bind_values'} ? : 24 8 32
111 0 ▶sub parameters { $_[0]->{parameters} ? @{$_[0]->{parameters}} : return ; }
Subroutine parameters: called CC=2 SCAR=9.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'parameters'} ? : 0 32 32
112  
113 252 ▶sub _dispatch {
Subroutine _dispatch: called CC=1 SCAR=0.0
114 252 $_[0]->{dispatch}{$_[1]}->(@_);
115 }
116  
117 171 ▶sub _quote_lit {
Subroutine _quote_lit: called CC=5 SCAR=16.2
118 171 my $arg = "$_[1]";
119 171 my $q = $_[0]->{config}{quote_lit};
120 171 ▶ if (looks_like_number $arg or
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($arg) or $arg =~ /^\s*$q(.*)$q\s*$/ or $arg =~ /$_[0]{'config'}{'parameter_sigil'}/ or builtin::blessed($_[1])) { } 72 99 171
Condition77%7/9
&looks_like_number($arg) or $arg =~ /^\s*$q(.*)$q\s*$/ or $arg =~ /$_[0]{"config"}{"parameter_sigil"}/ or builtin::blessed($_[1])
l!l&&r!l&&!r
70299
&looks_like_number($arg) or $arg =~ /^\s*$q(.*)$q\s*$/ or $arg =~ /$_[0]{"config"}{"parameter_sigil"}/
l!l&&r!l&&!r
700101
&looks_like_number($arg) or $arg =~ /^\s*$q(.*)$q\s*$/
l!l&&r!l&&!r
700101
MC/DC50%2/4
&looks_like_number($arg) or $arg =~ /^\s*$q(.*)$q\s*$/ or $arg =~ /$_[0]{"config"}{"parameter_sigil"}/ or builtin::blessed($_[1])
&looks_like_number($arg) $arg =~ /^\s*$q(.*)$q\s*$/ $arg =~ /$_[0]{"config"}{"parameter_sigil"}/ builtin::blessed($_[1])
Truth table60%3/5
A or B or C or D
A= &looks_like_number($arg)B= $arg =~ /^\s*$q(.*)$q\s*$/C= $arg =~ /$_[0]{"config"}{"parameter_sigil"}/D= builtin::blessed($_[1])
ABCDresult
1XXX1
01XX1
001X1
00011
00000
121 $arg =~ /^\s*$q(.*)$q\s*$/ or
122 $arg =~ $_[0]->{config}{parameter_sigil} or
123 blessed($_[1])
124 ) {
125 # numeric, already quoted, a parameter, or an object
126 72 return "$arg";
127 }
128 else {
129 99 my $e = $_[0]->{config}{esc_quote_lit};
130 99 $arg =~ s/$q/$e$q/g;
131 99 return "$q$arg$q";
132 }
133 }
134  
135 0 ▶sub _quote_fld { # noop
Subroutine _quote_fld: not called CC=1 SCAR=6.9
136 0 return $_[1];
137 }
138  
139 32 ▶sub express {
Subroutine express: called CC=2 SCAR=7.0
Pod documented
140 32 my $self = shift;
141 32 my $x = $_[0];
142 32 ▶ if ($SQL_ABSTRACT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SQL_ABSTRACT) 32 0 32
143 32 $x = $self->canonize($x);
144 }
145 32 return $self->peel($x);
146 }
147  
148 1 ▶sub config {
Subroutine config: called CC=3 SCAR=13.5
Pod documented
149 1 my $self = shift;
150 1 my ($key, $val) = @_;
151 1 ▶ if (!defined $key) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $key) { } 0 1 1
elsif (not defined $val) { } 0 1 1
152 0 return %{$self->{config}};
153 }
154 elsif (!defined $val) {
155 0 return $self->{config}{$key};
156 }
157 else {
158 1 return $self->{config}{$key} = $val;
159 }
160 }
161  
162  
163 # canonize - rewrite mixed hash/array expressions in canonical lispy
164 # array format - interpret like SQL::A
165 71 ▶sub canonize {
Subroutine canonize: called CC=1 SCAR=0.1
Pod documented
166 71 my $self = shift;
167 71 my ($expr) = @_;
168 71 my $ret = [];
169 71 my ($do,$is_op);
170 546 ▶ $is_op = sub {
Subroutine __ANON__: called CC=8 SCAR=21.3
171 546 ▶ if (!defined $_[0] || ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_[0] or ref $_[0]) 8 538 546
Condition66%2/3
not defined $_[0] or ref $_[0]
l!l&&r!l&&!r
08538
MC/DC50%1/2
not defined $_[0] or ref $_[0]
not defined $_[0] ref $_[0]
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= ref $_[0]
ABresult
1X1
011
000
172 8 return 0;
173 }
174 538 ▶ if (!$_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[1]) { } 215 323 538
175 215 ▶ if (defined $self->{dispatch}{$_[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'dispatch'}{$_[0]}) { } 63 152 215
176 63 1;
177 }
178 else {
179 152 ▶ puke "Unknown operator '$_[0]'" if (
Branch: 1 branch
BranchTrueFalseTotal
if not $_[0] =~ /$self->{"config"}{"safe_identifier"}/ and $_[0] =~ /^-|[[:punct:]]/ and not &looks_like_number($_[0]) 0 152 152
Condition50%3/6
not $_[0] =~ /$self->{"config"}{"safe_identifier"}/ and $_[0] =~ /^-|[[:punct:]]/ and not &looks_like_number($_[0])
!ll&&!rl&&r
15200
not $_[0] =~ /$self->{"config"}{"safe_identifier"}/ and $_[0] =~ /^-|[[:punct:]]/
!ll&&!rl&&r
14750
MC/DC0%0/3
not $_[0] =~ /$self->{"config"}{"safe_identifier"}/ and $_[0] =~ /^-|[[:punct:]]/ and not &looks_like_number($_[0])
not $_[0] =~ /$self->{"config"}{"safe_identifier"}/ $_[0] =~ /^-|[[:punct:]]/ not &looks_like_number($_[0])
Truth table50%2/4
not(A) and B and not(C)
A= not $_[0] =~ /$self->{"config"}{"safe_identifier"}/B= $_[0] =~ /^-|[[:punct:]]/C= not &looks_like_number($_[0])
ABCresult
0XX0
10X0
1100
1111
180 $_[0] !~ /$$self{config}{safe_identifier}/ and
181 $_[0]=~/^-|[[:punct:]]/ and !looks_like_number($_[0])
182 );
183 152 0;
184 }
185 }
186 else {
187 323 grep /^\Q$_[0]\E$/, @{$type_table{$_[1]}};
188 }
189 71 };
190 71 my $level=0;
191 346 ▶ $do = sub {
Subroutine __ANON__: called CC=65 SCAR=45.2
192 346 my ($expr, $lhs, $arg_of) = @_;
193 346 for (ref $expr) {
194 346 ▶ ($_ eq '' or blessed($expr)) && do {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "" || builtin::blessed($expr) and do { if (defined $expr) { return $self->{"config"}{"bind"} ? ["-bind", $expr] : $expr; } else { &puke("undef not interpretable"); } } 113 233 346
Condition100%3/3
$_ eq "" or builtin::blessed($expr)
l!l&&r!l&&!r
1121233
MC/DC100%2/2
$_ eq "" or builtin::blessed($expr)
$_ eq "" builtin::blessed($expr)
Truth table100%3/3
A or B
A= $_ eq ""B= builtin::blessed($expr)
ABresult
1X1
011
000
195 113 ▶ if (defined $expr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expr) { } 113 0 113
196 # literal (value?)
197 113 ▶ return $self->{config}{bind} ? [ -bind => $expr ] : $expr;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'config'}{'bind'} ? : 83 30 113
198 }
199 else {
200 0 puke "undef not interpretable";
201 }
202 };
203 233 ▶ /REF|SCALAR/ && do { # literals
Branch: 1 branch
BranchTrueFalseTotal
/REF|SCALAR/ and do { if ($_ eq "SCALAR") { return ["-thing", $$expr]; } elsif (ref $$expr eq "ARRAY") { my(@a) = @$$expr; my $thing = shift @a; @a = map({$self->_quote_lit($_);} @a); if ($self->{"config"}{"bind"}) { push @{$$self{"bind_values"};}, @a; } else { if ($self->{"config"}{"anon_placeholder"}) { $thing =~ s/\Q$self->{"config"}{"anon_placeholder"}\E/$_/ foreach (@a); } ; } ; return $lhs ? ["-thing", $lhs, ["-thing", $thing]] : ["-thing", $thing]; } } 22 211 233
204 22 ▶ if ($_ eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq 'SCALAR') { } 17 5 22
elsif (ref $$expr eq 'ARRAY') { } 5 0 5
205 17 return [-thing => $$expr] ; # never bind
206 }
207 elsif (ref $$expr eq 'ARRAY') {
208 # very SQL:A thing, but we'll do it
209 5 my @a = @$$expr;
210 5 my $thing = shift @a;
211 5 @a = map { $self->_quote_lit($_) } @a;
212 5 ▶ if ($self->{config}{bind}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'config'}{'bind'}) { } 0 5 5
213 0 push @{$self->{bind_values}}, @a;
214 }
215 else {
216 5 ▶ if ($self->{config}{anon_placeholder}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"config"}{"anon_placeholder"}) 4 1 5
217 4 ($thing =~ s/\Q$$self{config}{anon_placeholder}\E/$_/) for @a;
218 }
219 }
220 5 ▶ return $lhs ? [-thing => $lhs, [-thing => $thing]] : [-thing => $thing];
Branch: 1 branch
BranchTrueFalseTotal
$lhs ? : 3 2 5
221 }
222 };
223 211 ▶ /ARRAY/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/ARRAY/ and do { if (&$is_op($expr->[0], "infix_distributable")) { my $op = shift @$expr; my(@args, @flat); if (@$expr == 1) { foreach $_ (ref $expr->[0]) { /ARRAY/ and do { @flat = @{$expr->[0];}; last }; /HASH/ and do { @flat = %{$expr->[0];}; last }; &puke("Huh?"); } ; } else { @flat = @$expr; } ; while (@flat) { my $elt = shift @flat; if (not ref $elt) { push @args, &$do({$elt, shift @flat}, $lhs, $op); } else { next if ref $elt eq "ARRAY" and not scalar @$elt or ref $elt eq "HASH" and not scalar %$elt; push @args, &$do($elt, $lhs, $op); } ; } ; return [$op, @args]; } ; if (&$is_op($expr->[0]) and not &$is_op($expr->[0], "infix_distributable")) { return [$expr->[0], map({&$do($_, undef, $expr->[0]);} @$expr[1 .. $expr->$#*])]; } elsif (ref $expr->[0] eq "HASH") { return [$self->{"config"}{"array_op"}, map({&$do($_, $lhs, $self->{"config"}{"array_op"});} @$expr)]; } else { if ($lhs) { return [$self->{"config"}{"array_op"}, map({defined $_ ? [$self->{"config"}{"implicit_eq_op"}, $lhs, &$do($_, undef, $self->{"config"}{"implicit_eq_op"})] : ["-is_null", $lhs];} @$expr)]; } else { if ($arg_of and &any(sub { &$is_op($arg_of, $_); } , "function", "infix_listarg", "predicate", "reduce")) { return ["-list", map({&$do($_);} @$expr)]; } else { return &$do([$self->{"config"}{"array_op"}, @$expr]); } ; } ; } } 32 179 211
224 32 ▶ if ($is_op->($$expr[0],'infix_distributable')) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$is_op($expr->[0], "infix_distributable")) 12 20 32
225 # handle implicit equality pairs in an array
226 12 my $op = shift @$expr;
227 12 my (@args,@flat);
228 # flatten
229 12 ▶ if (@$expr == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$expr == 1) { } 3 9 12
230 3 for (ref($$expr[0])) {
231 3 ▶ /ARRAY/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/ARRAY/ and do { @flat = @{$expr->[0];}; last } 3 0 3
232 3 @flat = @{$$expr[0]};
233 3 last;
234 };
235 0 ▶ /HASH/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/HASH/ and do { @flat = %{$expr->[0];}; last } 0 0 0
236 0 @flat = %{$$expr[0]};
237 0 last;
238 };
239 0 puke 'Huh?';
240 };
241 }
242 else {
243 9 @flat = @$expr; # already flat
244 }
245 12 ▶ while (@flat) {
Branch: 1 branch
BranchTrueFalseTotal
if (@flat) 25 12 37
246 25 my $elt = shift @flat;
247 # $DB::single=1 if !defined($elt);
248 25 ▶ if (!ref $elt) { # scalar means lhs of a pair or another op
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $elt) { } 14 11 25
249 14 push @args, $do->({$elt => shift @flat},$lhs,$op);
250 }
251 else {
252 11 ▶ next if (ref $elt eq 'ARRAY') && ! scalar @$elt or
Branch: 1 branch
BranchTrueFalseTotal
if ref $elt eq "ARRAY" and not scalar @$elt or ref $elt eq "HASH" and not scalar %$elt 1 10 11
Condition77%7/9
ref $elt eq "ARRAY" and not scalar @$elt or ref $elt eq "HASH" and not scalar %$elt
l!l&&r!l&&!r
0110
ref $elt eq "ARRAY" and not scalar @$elt
!ll&&!rl&&r
830
ref $elt eq "HASH" and not scalar %$elt
!ll&&!rl&&r
641
MC/DC50%2/4
ref $elt eq "ARRAY" and not scalar @$elt or ref $elt eq "HASH" and not scalar %$elt
ref $elt eq "ARRAY" not scalar @$elt ref $elt eq "HASH" not scalar %$elt
Truth table57%4/7
A and not(B) or C and not(D)
A= ref $elt eq "ARRAY"B= not scalar @$eltC= ref $elt eq "HASH"D= not scalar %$elt
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
253 (ref $elt eq 'HASH') && ! scalar %$elt;
254 10 push @args, $do->($elt,$lhs,$op);
255 }
256 }
257 12 return [$op => @args];
258 }
259 20 ▶ if ($is_op->($$expr[0]) and !$is_op->($$expr[0],'infix_distributable')) {
Branch: 2 branches
BranchTrueFalseTotal
if (&$is_op($expr->[0]) and not &$is_op($expr->[0], 'infix_distributable')) { } 4 16 20
elsif (ref $expr->[0] eq 'HASH') { } 3 13 16
Condition66%2/3
&$is_op($expr->[0]) and not &$is_op($expr->[0], "infix_distributable")
!ll&&!rl&&r
1604
MC/DC50%1/2
&$is_op($expr->[0]) and not &$is_op($expr->[0], "infix_distributable")
&$is_op($expr->[0]) not &$is_op($expr->[0], "infix_distributable")
Truth table66%2/3
A and not(B)
A= &$is_op($expr->[0])B= not &$is_op($expr->[0], "infix_distributable")
ABresult
0X0
100
111
260 # some other op
261 return [ $$expr[0] => map {
262 4 $do->($_,undef,$$expr[0])
263 } @$expr[1..$#$expr] ];
264 }
265 elsif (ref $$expr[0] eq 'HASH') { #?
266 return [ $self->{config}{array_op} =>
267 3 map { $do->($_,$lhs,$self->{config}{array_op}) } @$expr ];
268 }
269 else { # is a plain list
270 13 ▶ if ($lhs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lhs) { } 4 9 13
271 # implicit equality over array default op
272 return [ $self->{config}{array_op} => map {
273 4 defined() ?
274 [ $self->{config}{implicit_eq_op} => $lhs,
275 13 ▶ $do->($_,undef,$self->{config}{implicit_eq_op}) ] :
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 12 1 13
276 [ -is_null => $lhs ]
277 } @$expr ];
278 }
279 else {
280 9 ▶ if ($arg_of and any { $is_op->($arg_of, $_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_of and &any(sub { &$is_op($arg_of, $_); } , 'function', 'infix_listarg', 'predicate', 'reduce')) { } 6 3 9
Condition66%2/3
$arg_of and &any(sub { &$is_op($arg_of, $_); } , "function", "infix_listarg", "predicate", "reduce")
!ll&&!rl&&r
306
MC/DC50%1/2
$arg_of and &any(sub { &$is_op($arg_of, $_); } , "function", "infix_listarg", "predicate", "reduce")
$arg_of &any(sub { &$is_op($arg_of, $_); } , "function", "infix_listarg", "predicate", "reduce")
Truth table66%2/3
A and B
A= $arg_ofB= &any(sub { &$is_op($arg_of, $_); } , "function", "infix_listarg", "predicate", "reduce")
ABresult
0X0
100
111
281 qw/function infix_listarg predicate reduce/
282 ) {
283 # function argument - return list itself
284 6 return [ -list => map { $do->($_) } @$expr ];
285 }
286 else {
287 # distribute $array_op over implicit equality
288 3 return $do->([ $self->{config}{array_op} => @$expr ]);
289 }
290 }
291 }
292 };
293 179 ▶ /HASH/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/HASH/ and do { my(@k) = keys %$expr; if (@k == 1) { my $k = $k[0]; if (&$is_op($k)) { &$is_op($k, "infix_binary") and do { &puke("Expected LHS for $k") unless $lhs; if (ref $expr->{$k} eq "ARRAY") { return [$self->{"config"}{"array_op"}, map({defined $_ ? [$k, $lhs, &$do($_)] : {$self->{"config"}{"ineq_op"}, ["-is_not_null", $lhs], $self->{"config"}{"implicit_eq_op"}, ["-is_null", $lhs]}->{$k};} @{$$expr{$k};})]; } elsif (defined $expr->{$k}) { return [$k, $lhs, &$do($expr->{$k}, undef, $k)]; } else { $k eq $self->{"config"}{"ineq_op"} and do { return ["-is_not_null", $lhs] }; $k eq $self->{"config"}{"implicit_eq_op"} and do { return ["-is_null", $lhs] }; &puke("Can't handle undef as argument to $k"); } }; &$is_op($k, "function") and do { return [$k, &$do($expr->{$k}, undef, $k)] }; &$is_op($k, "infix_listarg") and do { return [$k, $lhs, &$do($expr->{$k}, undef, $k)] }; &$is_op($k, "prefix") and do { return [$k, &$do($expr->{$k})] }; &$is_op($k, "infix_distributable") and do { if (not ref $expr->{$k} and $lhs) { return [$k, $lhs, &$do($expr->{$k})]; } elsif (ref $expr->{$k} eq "HASH") { my(@ar) = %{$expr->{$k};}; return &$do([$k, @ar]); } elsif (ref $expr->{$k} eq "ARRAY") { return &$do([$k, $expr->{$k}]); } else { &puke(q[arg type '] . ref($expr->{$k}) . "' not expected for op '${k}'"); } }; &$is_op($k, "predicate") and do { &puke("predicate function '${k}' requires an length 3 arrayref argument") unless ref $expr->{$k} eq "ARRAY" and @{$expr->{$k};} == 3; return [$k, ["-thing", $expr->{$k}[0]], &$do($expr->{$k}[1], undef, $k), &$do($expr->{$k}[2], undef, $k)] }; &$is_op($k, "reduce") and do { &puke("reduce function '${k}' requires an length 5 arrayref argument") unless ref $expr->{$k} eq "ARRAY" and @{$expr->{$k};} == 5; return [$k, ["-thing", $expr->{$k}[0]], &$do($expr->{$k}[1], undef, $k), ["-thing", $expr->{$k}[2]], &$do($expr->{$k}[3], undef, $k), &$do($expr->{$k}[4], undef, $k)] }; &puke("Operator $k not expected"); } elsif (ref $expr->{$k} and ref $expr->{$k} ne "SCALAR") { return &$do($expr->{$k}, $k, undef); } else { return defined $expr->{$k} ? [$self->{"config"}{"implicit_eq_op"}, $k, &$do($expr->{$k}, undef, $self->{"config"}{"implicit_eq_op"})] : ["-is_null", $k]; } ; } else { my @args; foreach my $k (@k) { if (&$is_op($k, "infix_binary")) { &puke(qq[No LHS provided for implicit $self->{"config"}{"hash_op"}]) unless defined $lhs; push @args, &$do({$k, $expr->{$k}}, $lhs); } elsif (&$is_op($k, "prefix") or &$is_op($k, "function")) { push @args, [$k, &$do($expr->{$k}, undef, $k)]; } elsif (not &$is_op($k)) { push @args, &$do({$k, $expr->{$k}}); } else { &puke("Problem handling operator '${k}'"); } ; } ; return [$self->{"config"}{"hash_op"}, @args]; } } 179 0 179
294 179 my @k = keys %$expr;
295 #######
296 179 ▶ if (@k == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@k == 1) { } 156 23 179
297 156 my $k = $k[0];
298 # single hashpair
299 156 ▶ if ($is_op->($k)) {
Branch: 2 branches
BranchTrueFalseTotal
if (&$is_op($k)) { } 59 97 156
elsif (ref $expr->{$k} and ref $expr->{$k} ne 'SCALAR') { } 38 59 97
300 59 ▶ $is_op->($k,'infix_binary') && do {
Branch: 1 branch
BranchTrueFalseTotal
&$is_op($k, "infix_binary") and do { &puke("Expected LHS for $k") unless $lhs; if (ref $expr->{$k} eq "ARRAY") { return [$self->{"config"}{"array_op"}, map({defined $_ ? [$k, $lhs, &$do($_)] : {$self->{"config"}{"ineq_op"}, ["-is_not_null", $lhs], $self->{"config"}{"implicit_eq_op"}, ["-is_null", $lhs]}->{$k};} @{$$expr{$k};})]; } elsif (defined $expr->{$k}) { return [$k, $lhs, &$do($expr->{$k}, undef, $k)]; } else { $k eq $self->{"config"}{"ineq_op"} and do { return ["-is_not_null", $lhs] }; $k eq $self->{"config"}{"implicit_eq_op"} and do { return ["-is_null", $lhs] }; &puke("Can't handle undef as argument to $k"); } } 33 26 59
301 33 ▶ puke "Expected LHS for $k" unless $lhs;
Branch: 1 branch
BranchTrueFalseTotal
unless $lhs 0 33 33
302 33 ▶ if (ref $$expr{$k} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $expr->{$k} eq 'ARRAY') { } 1 32 33
elsif (defined $expr->{$k}) { } 29 3 32
303 # apply binary operator and join with array default op
304 return [ $self->{config}{array_op} => map {
305 defined() ?
306 [ $k => $lhs, $do->($_)] :
307 { $self->{config}{ineq_op} => [-is_not_null => $lhs],
308 $self->{config}{implicit_eq_op} => [-is_null => $lhs]
309 2 ▶ }->{$k}
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 1 1 2
310 1 } @{$$expr{$k}} ]
311 }
312 elsif (defined $$expr{$k}) {
313 29 return [ $k => $lhs, $do->($$expr{$k},undef,$k) ]; #?
314 }
315 else { # IS (NOT) NULL
316 3 ▶ $k eq $self->{config}{ineq_op} && do {
Branch: 1 branch
BranchTrueFalseTotal
$k eq $self->{"config"}{"ineq_op"} and do { return ["-is_not_null", $lhs] } 3 0 3
317 3 return [ -is_not_null => $lhs ];
318 };
319 0 ▶ $k eq $self->{config}{implicit_eq_op} && do {
Branch: 1 branch
BranchTrueFalseTotal
$k eq $self->{"config"}{"implicit_eq_op"} and do { return ["-is_null", $lhs] } 0 0 0
320 0 return [ -is_null => $lhs ];
321 };
322 0 puke "Can't handle undef as argument to $k";
323 }
324 };
325 26 ▶ $is_op->($k,'function') && do {
Branch: 1 branch
BranchTrueFalseTotal
&$is_op($k, "function") and do { return [$k, &$do($expr->{$k}, undef, $k)] } 6 20 26
326 6 return [ $k => $do->($$expr{$k},undef,$k) ];
327 };
328 20 ▶ $is_op->($k,'infix_listarg') && do {
Branch: 1 branch
BranchTrueFalseTotal
&$is_op($k, "infix_listarg") and do { return [$k, $lhs, &$do($expr->{$k}, undef, $k)] } 4 16 20
329 4 return [ $k => $lhs, $do->($$expr{$k},undef,$k) ];
330 };
331 16 ▶ $is_op->($k,'prefix') && do {
Branch: 1 branch
BranchTrueFalseTotal
&$is_op($k, "prefix") and do { return [$k, &$do($expr->{$k})] } 8 8 16
332 8 return [ $k => $do->($$expr{$k}) ];
333 };
334 8 ▶ $is_op->($k,'infix_distributable') && do {
Branch: 1 branch
BranchTrueFalseTotal
&$is_op($k, "infix_distributable") and do { if (not ref $expr->{$k} and $lhs) { return [$k, $lhs, &$do($expr->{$k})]; } elsif (ref $expr->{$k} eq "HASH") { my(@ar) = %{$expr->{$k};}; return &$do([$k, @ar]); } elsif (ref $expr->{$k} eq "ARRAY") { return &$do([$k, $expr->{$k}]); } else { &puke(q[arg type '] . ref($expr->{$k}) . "' not expected for op '${k}'"); } } 4 4 8
335 4 ▶ if (!ref $$expr{$k} && $lhs) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $expr->{$k} and $lhs) { } 0 4 4
elsif (ref $expr->{$k} eq 'HASH') { } 1 3 4
elsif (ref $expr->{$k} eq 'ARRAY') { } 3 0 3
Condition33%1/3
not ref $expr->{$k} and $lhs
!ll&&!rl&&r
400
MC/DC0%0/2
not ref $expr->{$k} and $lhs
not ref $expr->{$k} $lhs
Truth table33%1/3
not(A) and B
A= not ref $expr->{$k}B= $lhs
ABresult
0X0
100
111
336 0 return [ $k => $lhs, $do->($$expr{$k}) ];
337 }
338 elsif ( ref $$expr{$k} eq 'HASH' ) {
339 1 my @ar = %{$$expr{$k}};
340 1 return $do->([$k=>@ar]); #?
341 }
342 elsif ( ref $$expr{$k} eq 'ARRAY') {
343 3 return $do->([$k => $$expr{$k}]);
344 }
345 else {
346 0 puke "arg type '".ref($$expr{$k})."' not expected for op '$k'";
347 }
348 };
349 4 ▶ $is_op->($k,'predicate') && do {
Branch: 1 branch
BranchTrueFalseTotal
&$is_op($k, "predicate") and do { &puke("predicate function '${k}' requires an length 3 arrayref argument") unless ref $expr->{$k} eq "ARRAY" and @{$expr->{$k};} == 3; return [$k, ["-thing", $expr->{$k}[0]], &$do($expr->{$k}[1], undef, $k), &$do($expr->{$k}[2], undef, $k)] } 2 2 4
350 puke "predicate function '$k' requires an length 3 arrayref argument"
351 2 ▶ unless ref $$expr{$k} eq 'ARRAY' and @{$$expr{$k}} == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $expr->{$k} eq "ARRAY" and @{$expr->{$k};} == 3 0 2 2
Condition33%1/3
ref $expr->{$k} eq "ARRAY" and @{$expr->{$k};} == 3
!ll&&!rl&&r
002
MC/DC0%0/2
ref $expr->{$k} eq "ARRAY" and @{$expr->{$k};} == 3
ref $expr->{$k} eq "ARRAY" @{$expr->{$k};} == 3
Truth table33%1/3
A and B
A= ref $expr->{$k} eq "ARRAY"B= @{$expr->{$k};} == 3
ABresult
0X0
100
111
352 return [ $k => [-thing => $$expr{$k}->[0]],
353 $do->($$expr{$k}->[1], undef, $k),
354 2 $do->($$expr{$k}->[2], undef, $k) ];
355 };
356 2 ▶ $is_op->($k,'reduce') && do {
Branch: 1 branch
BranchTrueFalseTotal
&$is_op($k, "reduce") and do { &puke("reduce function '${k}' requires an length 5 arrayref argument") unless ref $expr->{$k} eq "ARRAY" and @{$expr->{$k};} == 5; return [$k, ["-thing", $expr->{$k}[0]], &$do($expr->{$k}[1], undef, $k), ["-thing", $expr->{$k}[2]], &$do($expr->{$k}[3], undef, $k), &$do($expr->{$k}[4], undef, $k)] } 2 0 2
357 puke "reduce function '$k' requires an length 5 arrayref argument"
358 2 ▶ unless ref $$expr{$k} eq 'ARRAY' and @{$$expr{$k}} == 5;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $expr->{$k} eq "ARRAY" and @{$expr->{$k};} == 5 0 2 2
Condition33%1/3
ref $expr->{$k} eq "ARRAY" and @{$expr->{$k};} == 5
!ll&&!rl&&r
002
MC/DC0%0/2
ref $expr->{$k} eq "ARRAY" and @{$expr->{$k};} == 5
ref $expr->{$k} eq "ARRAY" @{$expr->{$k};} == 5
Truth table33%1/3
A and B
A= ref $expr->{$k} eq "ARRAY"B= @{$expr->{$k};} == 5
ABresult
0X0
100
111
359 return [ $k => [-thing => $$expr{$k}->[0]],
360 $do->($$expr{$k}->[1], undef, $k),
361 [-thing => $$expr{$k}->[2]],
362 $do->($$expr{$k}->[3], undef, $k),
363 2 $do->($$expr{$k}->[4], undef, $k)];
364 };
365 0 puke "Operator $k not expected";
366 }
367 ▶ elsif (ref($$expr{$k}) && ref($$expr{$k}) ne 'SCALAR') {
Condition100%3/3
ref $expr->{$k} and ref $expr->{$k} ne "SCALAR"
!ll&&!rl&&r
54538
MC/DC100%2/2
ref $expr->{$k} and ref $expr->{$k} ne "SCALAR"
ref $expr->{$k} ref $expr->{$k} ne "SCALAR"
Truth table100%3/3
A and B
A= ref $expr->{$k}B= ref $expr->{$k} ne "SCALAR"
ABresult
0X0
100
111
368 # $k is an LHS
369 38 return $do->($$expr{$k}, $k, undef);
370 }
371 else {
372 # implicit equality
373 return defined $$expr{$k} ?
374 [ $self->{config}{implicit_eq_op} => $k,
375 59 ▶ $do->($$expr{$k},undef,$self->{config}{implicit_eq_op}) ] :
Branch: 1 branch
BranchTrueFalseTotal
defined $expr->{$k} ? : 55 4 59
376 [ -is_null => $k ];
377 }
378 }
379 #######
380 else {
381 # >1 hashpair
382 23 my @args;
383 23 for my $k (@k) {
384 # all keys are ops, or none is - otherwise barf
385 52 ▶ if ( $is_op->($k, 'infix_binary') ) {
Branch: 3 branches
BranchTrueFalseTotal
if (&$is_op($k, 'infix_binary')) { } 6 46 52
elsif (&$is_op($k, 'prefix') or &$is_op($k, 'function')) { } 3 43 46
elsif (not &$is_op($k)) { } 43 0 43
386 6 ▶ puke "No LHS provided for implicit $$self{config}{hash_op}" unless defined $lhs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lhs 0 6 6
387 6 push @args, $do->({$k => $$expr{$k}},$lhs);
388 }
389 ▶ elsif ( $is_op->($k, 'prefix') || $is_op->($k,'function') ) {
Condition66%2/3
&$is_op($k, "prefix") or &$is_op($k, "function")
l!l&&r!l&&!r
3043
MC/DC50%1/2
&$is_op($k, "prefix") or &$is_op($k, "function")
&$is_op($k, "prefix") &$is_op($k, "function")
Truth table66%2/3
A or B
A= &$is_op($k, "prefix")B= &$is_op($k, "function")
ABresult
1X1
011
000
390 3 push @args, [ $k => $do->($$expr{$k},undef, $k) ];
391 }
392 elsif (!$is_op->($k)) {
393 43 push @args, $do->({$k => $$expr{$k}});
394 }
395 else {
396 0 puke "Problem handling operator '$k'";
397 }
398 }
399 23 return [ $self->{config}{hash_op} => @args ];
400 }
401 };
402 }
403 71 };
404 71 $ret = $do->($expr);
405 71 return $ret;
406 }
407  
408 # peel - recurse $args = [ -op, @args ] to create complete production
409 445 ▶sub peel {
Subroutine peel: called CC=8 SCAR=21.5
Pod documented
410 445 my ($self, $args) = @_;
411  
412 445 ▶ if (!defined $args) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $args) { } 0 445 445
elsif (not ref $args or builtin::blessed($args)) { } 193 252 445
elsif (ref $args eq 'ARRAY') { } 252 0 252
413 0 return '';
414 }
415 ▶ elsif (!ref $args or blessed($args)) { # single literal argument
Condition100%3/3
not ref $args or builtin::blessed($args)
l!l&&r!l&&!r
1921252
MC/DC100%2/2
not ref $args or builtin::blessed($args)
not ref $args builtin::blessed($args)
Truth table100%3/3
not(A) or B
A= not ref $argsB= builtin::blessed($args)
ABresult
1X1
011
000
416 193 return $args;
417 }
418 elsif (ref $args eq 'ARRAY') {
419 252 ▶ return '' unless (@$args);
Branch: 1 branch
BranchTrueFalseTotal
unless @$args 0 252 252
420 252 my $op = shift @$args;
421 252 ▶ puke "'$op' : unknown operator" unless $self->{dispatch}{$op};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dispatch"}{$op} 0 252 252
422 252 my $expr = $self->_dispatch( $op, [map { $self->peel($_) } @$args] );
423 252 ▶ if (grep /\Q$op\E/, @{$type_table{infix_distributable}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /\Q$op\E/, @{$type_table{'infix_distributable'};}) { } 34 218 252
424 # group
425 34 return "($expr)"
426 }
427 else {
428 218 return $expr;
429 }
430 }
431 else {
432 0 puke "Can only peel() arrayrefs, scalars or literals";
433 }
434 }
435  
436 ### writers
437  
438 78 ▶sub infix_binary {
Subroutine infix_binary: called CC=6 SCAR=24.2
Pod undocumented
439 78 my ($self, $op, $args) = @_;
440 78 ▶ unless ($op and $args and !ref($op)
Branch: 1 branch
BranchTrueFalseTotal
unless ($op and $args and not ref $op and ref $args eq "ARRAY") 0 78 78
Condition33%3/9
$op and $args and not ref $op and ref $args eq "ARRAY"
!ll&&!rl&&r
0078
$op and $args and not ref $op
!ll&&!rl&&r
0078
$op and $args
!ll&&!rl&&r
0078
MC/DC0%0/4
$op and $args and not ref $op and ref $args eq "ARRAY"
$op $args not ref $op ref $args eq "ARRAY"
Truth table20%1/5
A and B and not(C) and D
A= $opB= $argsC= not ref $opD= ref $args eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
441 and ref($args) eq 'ARRAY'){
442 0 puke "arg1 must be scalar, arg2 must be arrayref";
443 }
444 78 ▶ unless ( @$args == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$args == 2) 0 78 78
445 0 puke "For $op, arg2 must have length 2";
446 }
447 78 return '('.join(" ", $$args[0], _write_op($op), $$args[1]).')';
448 }
449  
450 3 ▶sub infix_listarg { infix_binary(@_) }
Subroutine infix_listarg: called CC=1 SCAR=0.0
Pod undocumented
451  
452 35 ▶sub infix_distributable {
Subroutine infix_distributable: called CC=5 SCAR=20.9
Pod undocumented
453 35 my ($self, $op, $args) = @_;
454 35 ▶ unless ($op and $args and !ref($op)
Branch: 1 branch
BranchTrueFalseTotal
unless ($op and $args and not ref $op and ref $args eq "ARRAY") 0 35 35
Condition33%3/9
$op and $args and not ref $op and ref $args eq "ARRAY"
!ll&&!rl&&r
0035
$op and $args and not ref $op
!ll&&!rl&&r
0035
$op and $args
!ll&&!rl&&r
0035
MC/DC0%0/4
$op and $args and not ref $op and ref $args eq "ARRAY"
$op $args not ref $op ref $args eq "ARRAY"
Truth table20%1/5
A and B and not(C) and D
A= $opB= $argsC= not ref $opD= ref $args eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
455 and ref($args) eq 'ARRAY'){
456 0 puke "arg1 must be scalar, arg2 must be arrayref";
457 }
458 35 $op = _write_op($op);
459 35 return join(" $op ", @$args);
460 }
461  
462 12 ▶sub prefix {
Subroutine prefix: called CC=6 SCAR=24.2
Pod undocumented
463 12 my ($self, $op, $args) = @_;
464 12 ▶ unless ($op and $args and !ref($op)
Branch: 1 branch
BranchTrueFalseTotal
unless ($op and $args and not ref $op and ref $args eq "ARRAY") 0 12 12
Condition33%3/9
$op and $args and not ref $op and ref $args eq "ARRAY"
!ll&&!rl&&r
0012
$op and $args and not ref $op
!ll&&!rl&&r
0012
$op and $args
!ll&&!rl&&r
0012
MC/DC0%0/4
$op and $args and not ref $op and ref $args eq "ARRAY"
$op $args not ref $op ref $args eq "ARRAY"
Truth table20%1/5
A and B and not(C) and D
A= $opB= $argsC= not ref $opD= ref $args eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
465 and ref($args) eq 'ARRAY'){
466 0 puke "arg1 must be scalar, arg2 must be arrayref";
467 }
468 12 ▶ unless (@$args == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$args == 1) 0 12 12
469 0 puke "For $op, arg2 must have length 1"
470 }
471 12 return _write_op($op)." ".$$args[0];
472 }
473  
474 6 ▶sub postfix {
Subroutine postfix: called CC=6 SCAR=24.2
Pod undocumented
475 6 my ($self, $op, $args) = @_;
476 6 ▶ unless ($op and $args and !ref($op)
Branch: 1 branch
BranchTrueFalseTotal
unless ($op and $args and not ref $op and ref $args eq "ARRAY") 0 6 6
Condition33%3/9
$op and $args and not ref $op and ref $args eq "ARRAY"
!ll&&!rl&&r
006
$op and $args and not ref $op
!ll&&!rl&&r
006
$op and $args
!ll&&!rl&&r
006
MC/DC0%0/4
$op and $args and not ref $op and ref $args eq "ARRAY"
$op $args not ref $op ref $args eq "ARRAY"
Truth table20%1/5
A and B and not(C) and D
A= $opB= $argsC= not ref $opD= ref $args eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
477 and ref($args) eq 'ARRAY'){
478 0 puke "arg1 must be scalar, arg2 must be arrayref";
479 }
480 6 ▶ unless (@$args == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$args == 1) 0 6 6
481 0 puke "For $op, arg2 must have length 1"
482 }
483 6 return $$args[0]." "._write_op($op);
484 }
485  
486 6 ▶sub function {
Subroutine function: called CC=5 SCAR=21.7
Pod undocumented
487 6 my ($self, $op, $args) = @_;
488 6 ▶ unless ($op and $args and !ref($op)
Branch: 1 branch
BranchTrueFalseTotal
unless ($op and $args and not ref $op and ref $args eq "ARRAY") 0 6 6
Condition33%3/9
$op and $args and not ref $op and ref $args eq "ARRAY"
!ll&&!rl&&r
006
$op and $args and not ref $op
!ll&&!rl&&r
006
$op and $args
!ll&&!rl&&r
006
MC/DC0%0/4
$op and $args and not ref $op and ref $args eq "ARRAY"
$op $args not ref $op ref $args eq "ARRAY"
Truth table20%1/5
A and B and not(C) and D
A= $opB= $argsC= not ref $opD= ref $args eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
489 and ref($args) eq 'ARRAY'){
490 0 puke "arg1 must be scalar, arg2 must be arrayref";
491 }
492 6 return _write_op($op).'('.join(',',@$args).')';
493 }
494  
495 2 ▶sub predicate {
Subroutine predicate: called CC=6 SCAR=24.2
Pod undocumented
496 2 my ($self, $op, $args) = @_;
497 2 ▶ unless ($op and $args and !ref($op)
Branch: 1 branch
BranchTrueFalseTotal
unless ($op and $args and not ref $op and ref $args eq "ARRAY") 0 2 2
Condition33%3/9
$op and $args and not ref $op and ref $args eq "ARRAY"
!ll&&!rl&&r
002
$op and $args and not ref $op
!ll&&!rl&&r
002
$op and $args
!ll&&!rl&&r
002
MC/DC0%0/4
$op and $args and not ref $op and ref $args eq "ARRAY"
$op $args not ref $op ref $args eq "ARRAY"
Truth table20%1/5
A and B and not(C) and D
A= $opB= $argsC= not ref $opD= ref $args eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
498 and ref($args) eq 'ARRAY'){
499 0 puke "arg1 must be scalar, arg2 must be arrayref";
500 }
501 2 ▶ unless ( @$args == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$args == 3) 0 2 2
502 0 puke "For $op, arg2 must have length 3";
503 }
504 2 return _write_op($op)."("."$$args[0] IN $$args[1] WHERE $$args[2]".")";
505 }
506  
507 0 ▶sub extract {
Subroutine extract: not called CC=6 SCAR=37.4
Pod undocumented
508 0 my ($self, $op, $args) = @_;
509 0 ▶ unless ($op and $args and !ref($op)
Branch: 1 branch
BranchTrueFalseTotal
unless ($op and $args and not ref $op and ref $args eq "ARRAY") 0 0 0
Condition0%0/9
$op and $args and not ref $op and ref $args eq "ARRAY"
!ll&&!rl&&r
000
$op and $args and not ref $op
!ll&&!rl&&r
000
$op and $args
!ll&&!rl&&r
000
MC/DC0%0/4
$op and $args and not ref $op and ref $args eq "ARRAY"
$op $args not ref $op ref $args eq "ARRAY"
Truth table0%0/5
A and B and not(C) and D
A= $opB= $argsC= not ref $opD= ref $args eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
510 and ref($args) eq 'ARRAY'){
511 0 puke "arg1 must be scalar, arg2 must be arrayref";
512 }
513 0 ▶ unless ( @$args == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$args == 3) 0 0 0
514 0 puke "For $op, arg2 must have length 3";
515 }
516 0 return _write_op($op)."("."$$args[0] IN $$args[1] | $$args[2]".")";
517 }
518  
519 2 ▶sub reduce {
Subroutine reduce: called CC=6 SCAR=24.2
Pod undocumented
520 2 my ($self, $op, $args) = @_;
521 2 ▶ unless ($op and $args and !ref($op)
Branch: 1 branch
BranchTrueFalseTotal
unless ($op and $args and not ref $op and ref $args eq "ARRAY") 0 2 2
Condition33%3/9
$op and $args and not ref $op and ref $args eq "ARRAY"
!ll&&!rl&&r
002
$op and $args and not ref $op
!ll&&!rl&&r
002
$op and $args
!ll&&!rl&&r
002
MC/DC0%0/4
$op and $args and not ref $op and ref $args eq "ARRAY"
$op $args not ref $op ref $args eq "ARRAY"
Truth table20%1/5
A and B and not(C) and D
A= $opB= $argsC= not ref $opD= ref $args eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
522 and ref($args) eq 'ARRAY'){
523 0 puke "arg1 must be scalar, arg2 must be arrayref";
524 }
525 2 ▶ unless ( @$args == 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$args == 5) 0 2 2
526 0 puke "For $op, arg2 must have length 5";
527 }
528 2 return _write_op($op)."("."$$args[0] = $$args[1], $$args[2] IN $$args[3] | $$args[4]".")";
529 }
530  
531 # bind either:
532 # - pulls out literals, pushes them into {bind_values}, and replaces them
533 # with '?' in the produced statement (a la SQL:A), -or-
534 # - identifies named parameters in the expression and pushes these into
535 # {parameters}, leaving them in the produced statement
536 83 ▶sub bind { # special
Subroutine bind: called CC=8 SCAR=27.7
Pod undocumented
537 83 my ($self, $op, $args) = @_;
538 83 ▶ unless ($op and $args and !ref($op)
Branch: 1 branch
BranchTrueFalseTotal
unless ($op and $args and not ref $op and ref $args eq "ARRAY") 0 83 83
Condition33%3/9
$op and $args and not ref $op and ref $args eq "ARRAY"
!ll&&!rl&&r
0083
$op and $args and not ref $op
!ll&&!rl&&r
0083
$op and $args
!ll&&!rl&&r
0083
MC/DC0%0/4
$op and $args and not ref $op and ref $args eq "ARRAY"
$op $args not ref $op ref $args eq "ARRAY"
Truth table20%1/5
A and B and not(C) and D
A= $opB= $argsC= not ref $opD= ref $args eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
539 and ref($args) eq 'ARRAY'){
540 0 puke "arg1 must be scalar, arg2 must be arrayref";
541 }
542 83 ▶ if ($$args[0] =~ $self->{config}{parameter_sigil}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->[0] =~ /$self->{'config'}{'parameter_sigil'}/) { } 0 83 83
543 0 push @{$self->{parameters}}, $$args[0];
544 }
545 else {
546 83 ▶ push @{$self->{bind_values}},
Branch: 1 branch
BranchTrueFalseTotal
$self->{'config'}{'anon_placeholder'} ? : 0 83 83
547 83 $self->{config}{anon_placeholder} ? $$args[0] :
548 $self->_quote_lit($$args[0]);
549 }
550 return $self->{config}{anon_placeholder} ?
551 $self->{config}{anon_placeholder} :
552 83 ▶ $self->_quote_lit($$args[0]);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'config'}{'anon_placeholder'} ? : 0 83 83
553 }
554  
555 5 ▶sub list { # special
Subroutine list: called CC=5 SCAR=20.9
Pod undocumented
556 5 my ($self, $op, $args) = @_;
557 5 ▶ unless ($op and $args and !ref($op)
Branch: 1 branch
BranchTrueFalseTotal
unless ($op and $args and not ref $op and ref $args eq "ARRAY") 0 5 5
Condition33%3/9
$op and $args and not ref $op and ref $args eq "ARRAY"
!ll&&!rl&&r
005
$op and $args and not ref $op
!ll&&!rl&&r
005
$op and $args
!ll&&!rl&&r
005
MC/DC0%0/4
$op and $args and not ref $op and ref $args eq "ARRAY"
$op $args not ref $op ref $args eq "ARRAY"
Truth table20%1/5
A and B and not(C) and D
A= $opB= $argsC= not ref $opD= ref $args eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
558 and ref($args) eq 'ARRAY'){
559 0 puke "arg1 must be scalar, arg2 must be arrayref";
560 }
561 5 my ($l,$r) = split '',$self->{config}{list_braces};
562 5 return $l.join(',',@$args).$r;
563 }
564  
565 29 ▶sub thing { # special
Subroutine thing: called CC=1 SCAR=0.0
Pod undocumented
566 29 my ($self, $op, $args) = @_;
567 29 return join(' ',@$args);
568 }
569  
570 141 ▶sub _write_op {
Subroutine _write_op: called CC=3 SCAR=11.0
571 141 my ($op) = @_;
572 141 $op =~ s/^-//;
573 141 my $c = (caller(1))[3];
574 141 ▶ return '' if ($op eq '()');
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "()" 0 141 141
575 141 ▶ return join(' ', map { ($c=~/infix|prefix|postfix/) ? uc $_ : $_ } split /_/,$op);
Branch: 1 branch
BranchTrueFalseTotal
$c =~ /infix|prefix|postfix/ ? : 145 10 155
576 }
577  
578 =head1 NAME
579  
580 Neo4j::Cypher::Abstract::Peeler - Parse Perl structures as expressions
581  
582 =head1 SYNOPSIS
583  
584 =head1 DESCRIPTION
585  
586 C<Neo4j::Cypher::Abstract::Peeler> allows the user to write L<Neo4j
587 Cypher|https://neo4j.com/docs/developer-manual/current/cypher/> query
588 language expressions as Perl data structures. The interpretation of
589 data structures follows L<SQL::Abstract> very closely, but attempts to
590 be more systematic and general.
591  
592 C<Peeler> only produces expressions, typically used as arguments to
593 C<WHERE> clauses. It is integrated into L<Neo4j::Cypher::Abstract>,
594 which produces full Cypher statements.
595  
596 Like L<SQL::Abstract>, C<Peeler> translates scalars, scalar refs,
597 array refs, and hash refs syntactically to create expression
598 components such as functions and operators. The contents of the
599 scalars or references are generally operators or the arguments to
600 operators or functions.
601  
602 Contents of scalar references are always treated as literals and
603 inserted into the expression verbatim.
604  
605 =head2 Expressing Expressions
606  
607 =over
608  
609 =item * Functions
610  
611 Ordinary functions in Cypher are written as the name of the function
612 preceded by a dash. They can be expressed as follows:
613  
614 { -func => $arg }
615 [ -func => $arg ]
616 \"func($arg)"
617  
618 { -sin => $pi/2 }
619 # returns sin(<value of $pi>/2)
620  
621 =item * Infix Operators
622  
623 Infix operators, like equality (C<=>), inequality (C<E<lt>E<gt>>),
624 binary operations (C<+,-,*,/>), and certain string operators
625 (C<-contains>, C<-starts_with>, C<ends_with>) are expressed as follows:
626  
627 { $expr1 => { $infix_op => $expr2 } }
628  
629 { 'n.name' => { '<>' => 'Fred' } }
630 # returns n.name <> "Fred"
631  
632 This may seem like overkill, but comes in handy for...
633  
634 =item * AND and OR
635  
636 C<Peeler> implements the L<SQL::Abstract> convention that hash refs
637 represent conditions joined by C<AND> and array refs represent
638 conditions joined by C<OR>. Key-value pairs and array value pairs are
639 interpreted as an implicit equalities to be ANDed or ORed.
640  
641 { $lhs1 => $rhs1, $lhs2 => $rhs2 }
642 { al => 'king', 'eddie' => 'prince', vicky => 'queen' }
643 # returns al = "king" AND eddie = "prince" AND vicky = "queen"
644  
645 [ $lhs1 => $rhs1, $lhs2 => $rhs2 ]
646 [ 'a.name' => 'Fred', 'a.name' => 'Barney']
647 # returns a.name = "Fred" OR a.name = "Barney"
648  
649 A single left-hand side can be "distributed" over a set of conditions,
650 with corresponding conjunction:
651  
652 { zzyxx => [ 'narf', 'boog', 'frelb' ] } # implicit equality, OR
653 # returns zzyxx = "narf" OR zzyxx = "boog" OR zzyxx = "frelb"
654 { zzyxx => { '<>' => 'narf', '<>' => 'boog' } } # explicit infix, AND
655 # returns zzyxx <> "narf" AND zzyxx <> "boog"
656 { zzyxx => [ '<>' => 'narf', -contains => 'boog' ] } # explicit infix, OR
657 # returns zzyxx <> "narf" OR zzyxx CONTAINS "boog"
658  
659 =item * Expressing null
660  
661 C<undef> can be used to express NULL mostly as in L<SQL::Abstract> so
662 that the following are equivalent
663  
664 { a.name => { '<>' => undef}, b.name => undef}
665 { -is_not_null => 'a.name', -is_null => 'b.name' }
666 # returns a.name IS NOT NULL AND b.name IS NULL
667  
668 =item * Predicates: -all, -any, -none, -single, -filter
669  
670 These Cypher functions have the form
671  
672 func(variable IN list WHERE predicate)
673  
674 To render these, provide an array ref of the three arguments in order:
675  
676 { -all => ['x', [1,2,3], {'x' => 3}] }
677 # returns all(x IN [1,2,3] WHERE x = 3)
678  
679 =item * List arguments
680  
681 For cypher expressions that accept lists (arrays in square brackets), use arrayrefs:
682  
683 { 'n.name' => { -in => ['fred','wilma','pebbles'] }}
684 # returns n.name IN ['fred','wilma','pebbles']
685  
686 =back
687  
688 =head2 Parameters and Bind Values
689  
690 Cypher parameters (which use the '$' sigil) may be included in
691 expressions (with the dollar sign appropriately escaped). These are
692 collected during parsing and can be reported in order with the the
693 C<parameters()> method.
694  
695 L<SQL::Abstract> automatically collects literal values and replaces
696 them with anonymous placeholders (C<?>), returning an array of values
697 for binding in L<DBI>. C<Peeler> will collect values and report them
698 with the C<bind_values()> method. If the config key
699 C<anon_placeholder> is set:
700  
701 $peeler->{config}{anon_placeholder} = '?'
702  
703 then C<Peeler> will also do the replacement in the final expression
704 production like L<SQL::Abstract>.
705  
706 The real reason to pay attention to literal values is to be able to
707 appropriately quote them in the final production. When
708 C<anon_placeholder> is not set (default), then an attempt is made to
709 correctly quote string values and such.
710  
711 =head1 GUTS
712  
713 TBD
714  
715 =head2 The config hash
716  
717 The C<Peeler> object property C<{config}> is a hashref containing
718 various defaults to make Peeler output sound like Cypher. One could
719 customize it to make output sound like SQL (someday).
720  
721 Keys/values are as follows:
722  
723 bind => 1,
724 anon_placeholder => undef, # '?',
725 hash_op => '-and',
726 array_op => '-or',
727 list_braces => '[]',
728 ineq_op => '<>',
729 implicit_eq_op => '=',
730 quote_lit => "'",
731 esc_quote_lit => "\\",
732 parameter_sigil => qw/^([:$?])|({[^}]+}$)/,
733 quote_fld => undef,
734 safe_identifier => qw/[a-zA-Z_.]+/
735  
736 =head1 METHODS
737  
738 =over
739  
740 =item express()
741  
742 Canonize, then peel. Returns scalar string (the expression).
743  
744 =item canonize()
745  
746 Render SQL:A-like expression into a canonical lisp-like array
747 tree. Returns an arrayref.
748  
749 =item peel()
750  
751 Render a canonical tree as a Cypher string expression. Returns scalar
752 string.
753  
754 =item parameters()
755  
756 Get a list in order of all named parameters.
757  
758 =item bind_values()
759  
760 Get a list of bind values in order that were replaced by the anonymous
761 placeholder.
762  
763 =back
764  
765 =head1 SEE ALSO
766  
767 L<Neo4j::Cypher::Abstract>, L<SQL::Abstract>.
768  
769 =head1 AUTHOR
770  
771 Mark A. Jensen
772 CPAN: MAJENSEN
773 majensen -at- cpan -dot- org
774  
775 =head1 LICENSE
776  
777 This software is provided for use under the terms of Perl itself.
778  
779 =head1 COPYRIGHT
780  
781 (c) 2017 Mark A. Jensen
782  
783 =cut
784  
785 6 1;
↑ Summary Abstract.pm »