blib/lib/MQUL.pm

filter: Statementstmt 93.2% 292 / 313 Branchbran 73.3% 251 / 342 Conditioncond 56.5% 130 / 230 MC/DCmcdc 24.8% 33 / 133 Subroutinesub 93.1% 27 / 29 Podpod 100% 2 / 2 totaltotal 70.0% 735 / 1049 SCAR 72.3
CC
283
Cov
76%
_attribute_matches
674.9
update_doc
171.8
doc_matches
24.7
CRAP 1383.9
↑ Summary
1 package MQUL;
2  
3 # ABSTRACT: General purpose, MongoDB-style query and update language
4  
5 BEGIN {
6 3 ▶ use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶ @EXPORT_OK = qw/doc_matches update_doc/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 }
9  
10 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Data::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Data::Types qw/:is/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use DateTime::Format::W3CDTF;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 our $VERSION = "2.000001";
20 3 $VERSION = eval $VERSION;
21  
22 =head1 NAME
23  
24 MQUL - General purpose, MongoDB-style query and update language
25  
26 =head1 SYNOPSIS
27  
28 use MQUL qw/doc_matches update_doc/;
29  
30 my $doc = {
31 title => 'Freaks and Geeks',
32 genres => [qw/comedy drama/],
33 imdb_score => 9.4,
34 seasons => 1,
35 starring => ['Linda Cardellini', 'James Franco', 'Jason Segel'],
36 likes => { up => 45, down => 11 }
37 };
38  
39 if (doc_matches($doc, {
40 title => qr/geeks/i,
41 genres => 'comedy',
42 imdb_score => { '$gte' => 5, '$lte' => 9.5 },
43 starring => { '$type' => 'array', '$size' => 3 },
44 'likes.up' => { '$gt' => 40 }
45 })) {
46 # will be true in this example
47 }
48  
49 update_doc($doc, {
50 '$set' => { title => 'Greeks and Feaks' },
51 '$pop' => { genres => 1 },
52 '$inc' => { imdb_score => 0.6 },
53 '$unset' => { seasons => 1 },
54 '$push' => { starring => 'John Francis Daley' },
55 });
56  
57 # $doc will now be:
58 {
59 title => 'Greeks and Feaks',
60 genres => ['comedy'],
61 imdb_score => 10,
62 starring => ['Linda Cardellini', 'James Franco', 'Jason Segel', 'John Francis Daley'],
63 likes => { up => 45, down => 11 }
64 }
65  
66 =head1 DESCRIPTION
67  
68 MQUL (for B<M>ongoDB-style B<Q>uery & B<U>pdate B<L>anguage; pronounced
69 I<"umm, cool">; yeah, I know, that's the dumbest thing ever), is a general
70 purpose implementation of L<MongoDB>'s query and update language. The
71 implementation is not 100% compatible, but it only slightly deviates from
72 MongoDB's behavior, actually extending it a bit.
73  
74 The module exports two subroutines: C<doc_matches()> and C<update_doc()>.
75 The first subroutine takes a document, which is really just a hash-ref (of
76 whatever complexity), and a query hash-ref built in the MQUL query language.
77 It returns a true value if the document matches the query, and a
78 false value otherwise. The second subroutine takes a document and an update
79 hash-ref built in the MQUL update language. The subroutine modifies the document
80 (in-place) according to the update hash-ref.
81  
82 You can use this module for whatever purpose you see fit. It was actually
83 written for L<Giddy>, my Git-database, and was extracted from its
84 original code. Outside of the database world, I plan to use it in an application
85 that performs tests (such as process monitoring for example), and uses the
86 query language to determine whether the results are valid or not (in our
87 monitoring example, that could be CPU usage above a certain threshold and
88 stuff like that). It is also used by L<MorboDB>, an in-memory clone of
89 MongoDB.
90  
91 =head2 UPGRADE NOTES
92  
93 My distributions follow the L<semantic versioning scheme|http://semver.org/>,
94 so whenever the major version changes, that means that API changes incompatible
95 with previous versions have been made. Always read the Changes file before upgrading.
96  
97 =head2 THE LANGUAGE
98  
99 The language itself is described in L<MQUL::Reference>. This document
100 only describes the interface of this module.
101  
102 The reference document also details MQUL's current differences from the
103 original MongoDB language.
104  
105 =cut
106  
107 our %BUILTINS = (
108 '$abs' => sub {
109 ##############################################
110 # abs( $value ) #
111 # ========================================== #
112 # $value - a numerical value #
113 # ------------------------------------------ #
114 # returns the absolute value of $value #
115 ##############################################
116 3 ▶ abs shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
117 },
118 '$min' => sub {
119 ##############################################
120 # min( @values ) #
121 # ========================================== #
122 # @values - a list of numerical values #
123 # ------------------------------------------ #
124 # returns the smallest number in @values #
125 ##############################################
126 4 ▶ my $min = shift;
Subroutine __ANON__: called CC=3 SCAR=11.1
127 4 foreach (@_) {
128 6 ▶ $min = $_ if $_ < $min;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < $min 0 6 6
129 }
130 4 return $min;
131 },
132 '$max' => sub {
133 ##############################################
134 # max( @values ) #
135 # ========================================== #
136 # @values - a list of numerical values #
137 # ------------------------------------------ #
138 # returns the largest number in @values #
139 ##############################################
140 6 ▶ my $max = shift;
Subroutine __ANON__: called CC=3 SCAR=11.1
141 6 foreach (@_) {
142 9 ▶ $max = $_ if $_ > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $_ > $max 9 0 9
143 }
144 6 return $max;
145 },
146 '$diff' => sub {
147 ##############################################
148 # diff( @values ) #
149 # ========================================== #
150 # @values - a list of numerical values #
151 # ------------------------------------------ #
152 # returns the difference between the values #
153 ##############################################
154 3 ▶ my $diff = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
155 3 foreach (@_) {
156 5 $diff -= $_;
157 }
158 3 return $diff;
159 },
160 '$sum' => sub {
161 ##############################################
162 # sum( @values ) #
163 # ========================================== #
164 # @values - a list of numerical values #
165 # ------------------------------------------ #
166 # returns the summation of the values #
167 ##############################################
168 2 ▶ my $sum = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
169 2 foreach (@_) {
170 3 $sum += $_;
171 }
172 2 return $sum;
173 },
174 '$product' => sub {
175 ##############################################
176 # product( @values ) #
177 # ========================================== #
178 # @values - a list of numerical values #
179 # ------------------------------------------ #
180 # returns the product of the values #
181 ##############################################
182 1 ▶ my $prod = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
183 1 foreach (@_) {
184 2 $prod *= $_;
185 }
186 1 return $prod;
187 },
188 '$div' => sub {
189 ##############################################
190 # div( @values ) #
191 # ========================================== #
192 # @values - a list of numerical values #
193 # ------------------------------------------ #
194 # returns the division of the values. #
195 # if the function encounters zero anywhere #
196 # after the first value, it will immediately #
197 # return zero instead of raise an error. #
198 ##############################################
199 2 ▶ my $div = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
200 2 foreach (@_) {
201 2 ▶ return 0 if $_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $_ == 0 1 1 2
202 1 $div /= $_;
203 }
204 1 return $div;
205 }
206 3 );
207  
208 =head1 INTERFACE
209  
210 =head2 doc_matches( \%document, [ \%query, \@defs ] )
211  
212 Receives a document hash-ref and possibly a query hash-ref, and returns
213 true if the document matches the query, false otherwise. If no query
214 is given (or an empty hash-ref is given), true will be returned (every
215 document will match an empty query - in accordance with MongoDB).
216  
217 See L<MQUL::Reference/"QUERY STRUCTURE"> to learn about the structure of
218 query hash-refs.
219  
220 Optionally, an even-numbered array reference of dynamically calculated
221 attribute definitions can be provided. For example:
222  
223 [ min_val => { '$min' => ['attr1', 'attr2', 'attr3' ] },
224 max_val => { '$max' => ['attr1', 'attr2', 'attr3' ] },
225 difference => { '$diff' => ['max_val', 'min_val'] } ]
226  
227 This defines three dynamic attributes: C<min_val>, C<max_val> and
228 C<difference>, which is made up of the first two.
229  
230 See L<MQUL::Reference/"DYNAMICALLY CALCULATED ATTRIBUTES"> for more information
231 about dynamic attributes.
232  
233 =cut
234  
235 sub doc_matches {
236 100 ▶ my ($doc, $query, $defs) = @_;
Subroutine doc_matches: called CC=24 SCAR=32.1
Pod documented
237  
238 100 ▶ croak 'MQUL::doc_matches() requires a document hash-ref.'
Branch: 1 branch
BranchTrueFalseTotal
unless $doc and ref $doc and ref $doc eq "HASH" 2 97 99
Condition100%6/6
$doc and ref $doc and ref $doc eq "HASH"
!ll&&!rl&&r
2197
$doc and ref $doc
!ll&&!rl&&r
1198
MC/DC100%3/3
$doc and ref $doc and ref $doc eq "HASH"
$doc ref $doc ref $doc eq "HASH"
Truth table100%4/4
A and B and C
A= $docB= ref $docC= ref $doc eq "HASH"
ABCresult
0XX0
10X0
1100
1111
239 unless $doc && ref $doc && ref $doc eq 'HASH';
240 97 ▶ croak 'MQUL::doc_matches() expects a query hash-ref.'
Branch: 1 branch
BranchTrueFalseTotal
if $query and !ref($query) || ref $query ne "HASH" 2 95 97
Condition100%6/6
$query and !ref($query) || ref $query ne "HASH"
!ll&&!rl&&r
1942
not ref $query or ref $query ne "HASH"
l!l&&r!l&&!r
1194
MC/DC66%2/3
$query and !ref($query) || ref $query ne "HASH"
$query not ref $query ref $query ne "HASH"
Truth table75%3/4
A and not(B) or C
A= $queryB= not ref $queryC= ref $query ne "HASH"
ABCresult
0XX0
1000
11X1
1011
241 if $query && (!ref $query || ref $query ne 'HASH');
242 95 ▶ croak 'MQUL::doc_matches() expects an even-numbered definitions array-ref.'
Branch: 1 branch
BranchTrueFalseTotal
if $defs and !ref($defs) || ref $defs ne "ARRAY" || scalar @$defs % 2 != 0 0 95 95
Condition44%4/9
$defs and !ref($defs) || ref $defs ne "ARRAY" || scalar @$defs % 2 != 0
!ll&&!rl&&r
76190
not ref $defs or ref $defs ne "ARRAY" or scalar @$defs % 2 != 0
l!l&&r!l&&!r
0019
not ref $defs or ref $defs ne "ARRAY"
l!l&&r!l&&!r
0019
MC/DC0%0/4
$defs and !ref($defs) || ref $defs ne "ARRAY" || scalar @$defs % 2 != 0
$defs not ref $defs ref $defs ne "ARRAY" scalar @$defs % 2 != 0
Truth table40%2/5
A and not(B) or C or D
A= $defsB= not ref $defsC= ref $defs ne "ARRAY"D= scalar @$defs % 2 != 0
ABCDresult
0XXX0
10000
11XX1
101X1
10011
243 if $defs && (!ref $defs || ref $defs ne 'ARRAY' || scalar @$defs % 2 != 0);
244  
245 95 ▶ $query ||= {};
Condition100%2/2
$query ||= {}
l!l
941
MC/DC100%1/1
$query ||= {}
$query
Truth table100%2/2
A
A= $query
Aresult
11
00
246  
247 95 ▶ if ($defs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defs) 19 76 95
248 19 ▶ for (my $i = 0; $i < scalar(@$defs) - 1; $i = $i + 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar(@$defs) - 1) 23 19 42
249 23 my ($name, $def) = ($defs->[$i], $defs->[$i+1]);
250 23 $doc->{$name} = _parse_function($doc, $def);
251 }
252 }
253  
254 # go over each key of the query
255 95 foreach my $key (keys %$query) {
256 112 my $value = $query->{$key};
257 112 ▶ if ($key eq '$or' && ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq '$or' and ref $value eq 'ARRAY') { } 11 101 112
Condition66%2/3
$key eq "\$or" and ref $value eq "ARRAY"
!ll&&!rl&&r
101011
MC/DC50%1/2
$key eq "\$or" and ref $value eq "ARRAY"
$key eq "\$or" ref $value eq "ARRAY"
Truth table66%2/3
A and B
A= $key eq "\$or"B= ref $value eq "ARRAY"
ABresult
0X0
100
111
258 11 my $found;
259 11 foreach (@$value) {
260 18 ▶ next unless ref $_ eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "HASH" 0 18 18
261 18 my $ok = 1;
262  
263 18 ▶ while (my ($k, $v) = each %$_) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$_) 19 7 26
264 19 ▶ unless (&_attribute_matches($doc, $k, $v)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_attribute_matches($doc, $k, $v)) 11 8 19
265 11 undef $ok;
266 11 last;
267 }
268 }
269  
270 18 ▶ if ($ok) { # document matches this criteria
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 7 11 18
271 7 $found = 1;
272 7 last;
273 }
274 }
275 11 ▶ return unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 4 7 11
276 } else {
277 101 ▶ return unless &_attribute_matches($doc, $key, $value);
Branch: 1 branch
BranchTrueFalseTotal
unless &_attribute_matches($doc, $key, $value) 25 76 101
278 }
279 }
280  
281 # if we've reached here, the document matches, so return true
282 66 return 1;
283 }
284  
285 ##############################################
286 # _attribute_matches( $doc, $key, $value ) #
287 # ========================================== #
288 # $doc - the document hash-ref #
289 # $key - the attribute being checked #
290 # $value - the constraint for the attribute #
291 # taken from the query hash-ref #
292 # ------------------------------------------ #
293 # returns true if constraint is met in the #
294 # provided document. #
295 ##############################################
296  
297 3 my $funcs = join('|', keys %BUILTINS);
298  
299 sub _attribute_matches {
300 120 ▶ my ($doc, $key, $value) = @_;
Subroutine _attribute_matches: called CC=132 SCAR=65.1
301  
302 120 my %virt;
303 120 ▶ if ($key =~ m/\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\./) { } 17 103 120
304 # support for the dot notation
305 17 my ($v, $k) = _expand_dot_notation($doc, $key);
306  
307 17 $key = $k;
308 17 ▶ $virt{$key} = $v
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 13 4 17
309 if defined $v;
310 } else {
311 $virt{$key} = $doc->{$key}
312 103 ▶ if exists $doc->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $doc->{$key} 97 6 103
313 }
314  
315 120 ▶ if (!ref $value) { # if value is a scalar, we need to check for equality
Branch: 5 branches
BranchTrueFalseTotal
if (not ref $value) { } 27 93 120
elsif (builtin::blessed($value) and builtin::blessed($value) eq 'MongoDB::OID' || builtin::blessed($value) eq 'MorboDB::OID') { } 0 93 93
elsif (ref $value eq 'Regexp') { } 2 91 93
elsif (ref $value eq 'HASH') { } 90 1 91
elsif (ref $value eq 'ARRAY') { } 1 0 1
316 # (or, if the attribute is an array in the document,
317 # we need to check the value exists in it)
318 27 ▶ return unless defined $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} 2 25 27
319 25 ▶ if (ref $virt{$key} eq 'ARRAY') { # check the array has the requested value
Branch: 2 branches
BranchTrueFalseTotal
if (ref $virt{$key} eq 'ARRAY') { } 0 25 25
elsif (not ref $virt{$key}) { } 25 0 25
320 0 ▶ return unless &_array_has_eq($value, $virt{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless &_array_has_eq($value, $virt{$key}) 0 0 0
321 } elsif (!ref $virt{$key}) { # check the values are equal
322 25 ▶ return unless $virt{$key} eq $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} eq $value 7 18 25
323 } else { # we can't compare a non-scalar to a scalar, so return false
324 0 return;
325 }
326 ▶ } elsif (blessed $value && (blessed $value eq 'MongoDB::OID' || blessed $value eq 'MorboDB::OID')) {
Condition50%3/6
builtin::blessed($value) and builtin::blessed($value) eq "MongoDB::OID" || builtin::blessed($value) eq "MorboDB::OID"
!ll&&!rl&&r
9120
builtin::blessed($value) eq "MongoDB::OID" or builtin::blessed($value) eq "MorboDB::OID"
l!l&&r!l&&!r
002
MC/DC0%0/3
builtin::blessed($value) and builtin::blessed($value) eq "MongoDB::OID" || builtin::blessed($value) eq "MorboDB::OID"
builtin::blessed($value) builtin::blessed($value) eq "MongoDB::OID" builtin::blessed($value) eq "MorboDB::OID"
Truth table50%2/4
A and B or C
A= builtin::blessed($value)B= builtin::blessed($value) eq "MongoDB::OID"C= builtin::blessed($value) eq "MorboDB::OID"
ABCresult
0XX0
1000
11X1
1011
327 # we're trying to compare MongoDB::OIDs/MorboDB::OIDs
328 # (MorboDB is my in-memory clone of MongoDB)
329 0 ▶ return unless defined $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} 0 0 0
330 0 ▶ if (blessed $virt{$key} && (blessed $virt{$key} eq 'MongoDB::OID' || blessed $virt{$key} eq 'MorboDB::OID')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($virt{$key}) and builtin::blessed($virt{$key}) eq 'MongoDB::OID' || builtin::blessed($virt{$key}) eq 'MorboDB::OID') { } 0 0 0
Condition0%0/6
builtin::blessed($virt{$key}) and builtin::blessed($virt{$key}) eq "MongoDB::OID" || builtin::blessed($virt{$key}) eq "MorboDB::OID"
!ll&&!rl&&r
000
builtin::blessed($virt{$key}) eq "MongoDB::OID" or builtin::blessed($virt{$key}) eq "MorboDB::OID"
l!l&&r!l&&!r
000
MC/DC0%0/3
builtin::blessed($virt{$key}) and builtin::blessed($virt{$key}) eq "MongoDB::OID" || builtin::blessed($virt{$key}) eq "MorboDB::OID"
builtin::blessed($virt{$key}) builtin::blessed($virt{$key}) eq "MongoDB::OID" builtin::blessed($virt{$key}) eq "MorboDB::OID"
Truth table0%0/4
A and B or C
A= builtin::blessed($virt{$key})B= builtin::blessed($virt{$key}) eq "MongoDB::OID"C= builtin::blessed($virt{$key}) eq "MorboDB::OID"
ABCresult
0XX0
1000
11X1
1011
331 0 ▶ return unless $virt{$key}->value eq $value->value;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key}->value eq $value->value 0 0 0
332 } else {
333 0 return;
334 }
335 } elsif (ref $value eq 'Regexp') { # if the value is a regex, we need to check
336 # for a match (or, if the attribute is an array
337 # in the document, we need to check at least one
338 # value in it matches it)
339 2 ▶ return unless defined $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} 0 2 2
340 2 ▶ if (ref $virt{$key} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $virt{$key} eq 'ARRAY') { } 0 2 2
elsif (not ref $virt{$key}) { } 2 0 2
341 0 ▶ return unless &_array_has_re($value, $virt{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless &_array_has_re($value, $virt{$key}) 0 0 0
342 } elsif (!ref $virt{$key}) { # check the values match
343 2 ▶ return unless $virt{$key} =~ $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} =~ /$value/ 1 1 2
344 } else { # we can't compare a non-scalar to a scalar, so return false
345 0 return;
346 }
347 } elsif (ref $value eq 'HASH') { # if the value is a hash, than it either contains
348 # advanced queries, or it's just a hash that we
349 # want the document to have as-is
350 90 ▶ unless (&_has_adv_que($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_has_adv_que($value)) { } 88 2 90
351 # value hash-ref doesn't have any advanced
352 # queries, we need to check our document
353 # has an attributes with exactly the same hash-ref
354 # (and name of course)
355 2 ▶ return unless Compare($value, $virt{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless Compare($value, $virt{$key}) 0 2 2
356 } else {
357 # value contains advanced queries,
358 # we need to make sure our document has an
359 # attribute with the same name that matches
360 # all these queries
361 88 foreach my $q (keys %$value) {
362 97 my $term = $value->{$q};
363 97 ▶ if ($q eq '$gt') {
Branch: 13 branches
BranchTrueFalseTotal
if ($q eq '$gt') { } 14 83 97
elsif ($q eq '$gte') { } 7 76 83
elsif ($q eq '$lt') { } 7 69 76
elsif ($q eq '$lte') { } 5 64 69
elsif ($q eq '$eq') { } 1 63 64
elsif ($q eq '$ne') { } 2 61 63
elsif ($q eq '$exists') { } 15 46 61
elsif ($q eq '$mod' and ref $term eq 'ARRAY' and scalar @$term == 2) { } 4 42 46
elsif ($q eq '$in' and ref $term eq 'ARRAY') { } 4 38 42
elsif ($q eq '$nin' and ref $term eq 'ARRAY') { } 3 35 38
elsif ($q eq '$size' and &is_int($term)) { } 4 31 35
elsif ($q eq '$all' and ref $term eq 'ARRAY') { } 3 28 31
elsif ($q eq '$type' and not ref $term) { } 28 0 28
364 14 ▶ return unless defined $virt{$key} && !ref $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and not ref $virt{$key} 0 14 14
Condition33%1/3
defined $virt{$key} and not ref $virt{$key}
!ll&&!rl&&r
0014
MC/DC0%0/2
defined $virt{$key} and not ref $virt{$key}
defined $virt{$key} not ref $virt{$key}
Truth table33%1/3
A and not(B)
A= defined $virt{$key}B= not ref $virt{$key}
ABresult
0X0
100
111
365 14 ▶ if (is_float($virt{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_float($virt{$key})) { } 14 0 14
366 14 ▶ return unless $virt{$key} > $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} > $term 7 7 14
367 } else {
368 0 ▶ return unless $virt{$key} gt $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} gt $term 0 0 0
369 }
370 } elsif ($q eq '$gte') {
371 7 ▶ return unless defined $virt{$key} && !ref $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and not ref $virt{$key} 0 7 7
Condition33%1/3
defined $virt{$key} and not ref $virt{$key}
!ll&&!rl&&r
007
MC/DC0%0/2
defined $virt{$key} and not ref $virt{$key}
defined $virt{$key} not ref $virt{$key}
Truth table33%1/3
A and not(B)
A= defined $virt{$key}B= not ref $virt{$key}
ABresult
0X0
100
111
372 7 ▶ if (is_float($virt{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_float($virt{$key})) { } 7 0 7
373 7 ▶ return unless $virt{$key} >= $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} >= $term 1 6 7
374 } else {
375 0 ▶ return unless $virt{$key} ge $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} ge $term 0 0 0
376 }
377 } elsif ($q eq '$lt') {
378 7 ▶ return unless defined $virt{$key} && !ref $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and not ref $virt{$key} 0 7 7
Condition33%1/3
defined $virt{$key} and not ref $virt{$key}
!ll&&!rl&&r
007
MC/DC0%0/2
defined $virt{$key} and not ref $virt{$key}
defined $virt{$key} not ref $virt{$key}
Truth table33%1/3
A and not(B)
A= defined $virt{$key}B= not ref $virt{$key}
ABresult
0X0
100
111
379 7 ▶ if (is_float($virt{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_float($virt{$key})) { } 6 1 7
380 6 ▶ return unless $virt{$key} < $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} < $term 1 5 6
381 } else {
382 1 ▶ return unless $virt{$key} lt $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} lt $term 0 1 1
383 }
384 } elsif ($q eq '$lte') {
385 5 ▶ return unless defined $virt{$key} && !ref $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and not ref $virt{$key} 0 5 5
Condition33%1/3
defined $virt{$key} and not ref $virt{$key}
!ll&&!rl&&r
005
MC/DC0%0/2
defined $virt{$key} and not ref $virt{$key}
defined $virt{$key} not ref $virt{$key}
Truth table33%1/3
A and not(B)
A= defined $virt{$key}B= not ref $virt{$key}
ABresult
0X0
100
111
386 5 ▶ if (is_float($virt{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_float($virt{$key})) { } 5 0 5
387 5 ▶ return unless $virt{$key} <= $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} <= $term 1 4 5
388 } else {
389 0 ▶ return unless $virt{$key} le $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} le $term 0 0 0
390 }
391 } elsif ($q eq '$eq') {
392 1 ▶ return unless defined $virt{$key} && !ref $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and not ref $virt{$key} 0 1 1
Condition33%1/3
defined $virt{$key} and not ref $virt{$key}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $virt{$key} and not ref $virt{$key}
defined $virt{$key} not ref $virt{$key}
Truth table33%1/3
A and not(B)
A= defined $virt{$key}B= not ref $virt{$key}
ABresult
0X0
100
111
393 1 ▶ if (is_float($virt{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_float($virt{$key})) { } 0 1 1
394 0 ▶ return unless $virt{$key} == $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} == $term 0 0 0
395 } else {
396 1 ▶ return unless $virt{$key} eq $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} eq $term 0 1 1
397 }
398 } elsif ($q eq '$ne') {
399 2 ▶ return unless defined $virt{$key} && !ref $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and not ref $virt{$key} 0 2 2
Condition33%1/3
defined $virt{$key} and not ref $virt{$key}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $virt{$key} and not ref $virt{$key}
defined $virt{$key} not ref $virt{$key}
Truth table33%1/3
A and not(B)
A= defined $virt{$key}B= not ref $virt{$key}
ABresult
0X0
100
111
400 2 ▶ if (is_float($virt{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_float($virt{$key})) { } 1 1 2
401 1 ▶ return unless $virt{$key} != $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} != $term 0 1 1
402 } else {
403 1 ▶ return unless $virt{$key} ne $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} ne $term 0 1 1
404 }
405 } elsif ($q eq '$exists') {
406 15 ▶ if ($term) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term) { } 10 5 15
407 10 ▶ return unless exists $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $virt{$key} 5 5 10
408 } else {
409 5 ▶ return if exists $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $virt{$key} 1 4 5
410 }
411 ▶ } elsif ($q eq '$mod' && ref $term eq 'ARRAY' && scalar @$term == 2) {
Condition66%4/6
$q eq "\$mod" and ref $term eq "ARRAY" and scalar @$term == 2
!ll&&!rl&&r
4204
$q eq "\$mod" and ref $term eq "ARRAY"
!ll&&!rl&&r
4204
MC/DC33%1/3
$q eq "\$mod" and ref $term eq "ARRAY" and scalar @$term == 2
$q eq "\$mod" ref $term eq "ARRAY" scalar @$term == 2
Truth table50%2/4
A and B and C
A= $q eq "\$mod"B= ref $term eq "ARRAY"C= scalar @$term == 2
ABCresult
0XX0
10X0
1100
1111
412 4 ▶ return unless defined $virt{$key} && is_float($virt{$key}) && $virt{$key} % $term->[0] == $term->[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and &is_float($virt{$key}) and $virt{$key} % $term->[0] == $term->[1] 1 3 4
Condition50%3/6
defined $virt{$key} and &is_float($virt{$key}) and $virt{$key} % $term->[0] == $term->[1]
!ll&&!rl&&r
013
defined $virt{$key} and &is_float($virt{$key})
!ll&&!rl&&r
004
MC/DC33%1/3
defined $virt{$key} and &is_float($virt{$key}) and $virt{$key} % $term->[0] == $term->[1]
defined $virt{$key} &is_float($virt{$key}) $virt{$key} % $term->[0] == $term->[1]
Truth table50%2/4
A and B and C
A= defined $virt{$key}B= &is_float($virt{$key})C= $virt{$key} % $term->[0] == $term->[1]
ABCresult
0XX0
10X0
1100
1111
413 ▶ } elsif ($q eq '$in' && ref $term eq 'ARRAY') {
Condition66%2/3
$q eq "\$in" and ref $term eq "ARRAY"
!ll&&!rl&&r
3804
MC/DC50%1/2
$q eq "\$in" and ref $term eq "ARRAY"
$q eq "\$in" ref $term eq "ARRAY"
Truth table66%2/3
A and B
A= $q eq "\$in"B= ref $term eq "ARRAY"
ABresult
0X0
100
111
414 4 ▶ return unless defined $virt{$key} && &_value_in($virt{$key}, $term);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and &_value_in($virt{$key}, $term) 1 3 4
Condition66%2/3
defined $virt{$key} and &_value_in($virt{$key}, $term)
!ll&&!rl&&r
013
MC/DC50%1/2
defined $virt{$key} and &_value_in($virt{$key}, $term)
defined $virt{$key} &_value_in($virt{$key}, $term)
Truth table66%2/3
A and B
A= defined $virt{$key}B= &_value_in($virt{$key}, $term)
ABresult
0X0
100
111
415 ▶ } elsif ($q eq '$nin' && ref $term eq 'ARRAY') {
Condition66%2/3
$q eq "\$nin" and ref $term eq "ARRAY"
!ll&&!rl&&r
3503
MC/DC50%1/2
$q eq "\$nin" and ref $term eq "ARRAY"
$q eq "\$nin" ref $term eq "ARRAY"
Truth table66%2/3
A and B
A= $q eq "\$nin"B= ref $term eq "ARRAY"
ABresult
0X0
100
111
416 3 ▶ return unless defined $virt{$key} && !&_value_in($virt{$key}, $term);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and not &_value_in($virt{$key}, $term) 1 2 3
Condition66%2/3
defined $virt{$key} and not &_value_in($virt{$key}, $term)
!ll&&!rl&&r
012
MC/DC50%1/2
defined $virt{$key} and not &_value_in($virt{$key}, $term)
defined $virt{$key} not &_value_in($virt{$key}, $term)
Truth table66%2/3
A and not(B)
A= defined $virt{$key}B= not &_value_in($virt{$key}, $term)
ABresult
0X0
100
111
417 ▶ } elsif ($q eq '$size' && is_int($term)) {
Condition66%2/3
$q eq "\$size" and &is_int($term)
!ll&&!rl&&r
3104
MC/DC50%1/2
$q eq "\$size" and &is_int($term)
$q eq "\$size" &is_int($term)
Truth table66%2/3
A and B
A= $q eq "\$size"B= &is_int($term)
ABresult
0X0
100
111
418 4 ▶ return unless defined $virt{$key} && ((ref $virt{$key} eq 'ARRAY' && scalar @{$virt{$key}} == $term) || (ref $virt{$key} eq 'HASH' && scalar keys %{$virt{$key}} == $term));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and ref $virt{$key} eq "ARRAY" && scalar @{$virt{$key};} == $term || ref $virt{$key} eq "HASH" && scalar keys %{$virt{$key};} == $term 1 3 4
Condition75%9/12
defined $virt{$key} and ref $virt{$key} eq "ARRAY" && scalar @{$virt{$key};} == $term || ref $virt{$key} eq "HASH" && scalar keys %{$virt{$key};} == $term
!ll&&!rl&&r
013
ref $virt{$key} eq "ARRAY" and scalar @{$virt{$key};} == $term or ref $virt{$key} eq "HASH" and scalar keys %{$virt{$key};} == $term
l!l&&r!l&&!r
220
ref $virt{$key} eq "ARRAY" and scalar @{$virt{$key};} == $term
!ll&&!rl&&r
112
ref $virt{$key} eq "HASH" and scalar keys %{$virt{$key};} == $term
!ll&&!rl&&r
101
MC/DC0%0/5
defined $virt{$key} and ref $virt{$key} eq "ARRAY" && scalar @{$virt{$key};} == $term || ref $virt{$key} eq "HASH" && scalar keys %{$virt{$key};} == $term
defined $virt{$key} ref $virt{$key} eq "ARRAY" scalar @{$virt{$key};} == $term ref $virt{$key} eq "HASH" scalar keys %{$virt{$key};} == $term
Truth table12%1/8
A and B and C or D and E
A= defined $virt{$key}B= ref $virt{$key} eq "ARRAY"C= scalar @{$virt{$key};} == $termD= ref $virt{$key} eq "HASH"E= scalar keys %{$virt{$key};} == $term
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
419 ▶ } elsif ($q eq '$all' && ref $term eq 'ARRAY') {
Condition66%2/3
$q eq "\$all" and ref $term eq "ARRAY"
!ll&&!rl&&r
2803
MC/DC50%1/2
$q eq "\$all" and ref $term eq "ARRAY"
$q eq "\$all" ref $term eq "ARRAY"
Truth table66%2/3
A and B
A= $q eq "\$all"B= ref $term eq "ARRAY"
ABresult
0X0
100
111
420 3 ▶ return unless defined $virt{$key} && ref $virt{$key} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and ref $virt{$key} eq "ARRAY" 0 3 3
Condition33%1/3
defined $virt{$key} and ref $virt{$key} eq "ARRAY"
!ll&&!rl&&r
003
MC/DC0%0/2
defined $virt{$key} and ref $virt{$key} eq "ARRAY"
defined $virt{$key} ref $virt{$key} eq "ARRAY"
Truth table33%1/3
A and B
A= defined $virt{$key}B= ref $virt{$key} eq "ARRAY"
ABresult
0X0
100
111
421 3 foreach (@$term) {
422 6 ▶ return unless &_value_in($_, $virt{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless &_value_in($_, $virt{$key}) 1 5 6
423 }
424 ▶ } elsif ($q eq '$type' && !ref $term) {
Condition33%1/3
$q eq "\$type" and not ref $term
!ll&&!rl&&r
0028
MC/DC0%0/2
$q eq "\$type" and not ref $term
$q eq "\$type" not ref $term
Truth table33%1/3
A and not(B)
A= $q eq "\$type"B= not ref $term
ABresult
0X0
100
111
425 28 ▶ if ($term eq 'int') {
Branch: 11 branches
BranchTrueFalseTotal
if ($term eq 'int') { } 3 25 28
elsif ($term eq 'float') { } 4 21 25
elsif ($term eq 'real') { } 2 19 21
elsif ($term eq 'whole') { } 2 17 19
elsif ($term eq 'string') { } 2 15 17
elsif ($term eq 'array') { } 2 13 15
elsif ($term eq 'hash') { } 5 8 13
elsif ($term eq 'bool') { } 1 7 8
elsif ($term eq 'date') { } 3 4 7
elsif ($term eq 'null') { } 3 1 4
elsif ($term eq 'regex') { } 1 0 1
426 3 ▶ return unless defined $virt{$key} && is_int($virt{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and &is_int($virt{$key}) 1 2 3
Condition66%2/3
defined $virt{$key} and &is_int($virt{$key})
!ll&&!rl&&r
012
MC/DC50%1/2
defined $virt{$key} and &is_int($virt{$key})
defined $virt{$key} &is_int($virt{$key})
Truth table66%2/3
A and B
A= defined $virt{$key}B= &is_int($virt{$key})
ABresult
0X0
100
111
427 } elsif ($term eq 'float') {
428 4 ▶ return unless defined $virt{$key} && is_float($virt{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and &is_float($virt{$key}) 0 4 4
Condition33%1/3
defined $virt{$key} and &is_float($virt{$key})
!ll&&!rl&&r
004
MC/DC0%0/2
defined $virt{$key} and &is_float($virt{$key})
defined $virt{$key} &is_float($virt{$key})
Truth table33%1/3
A and B
A= defined $virt{$key}B= &is_float($virt{$key})
ABresult
0X0
100
111
429 } elsif ($term eq 'real') {
430 2 ▶ return unless defined $virt{$key} && is_real($virt{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and &is_real($virt{$key}) 0 2 2
Condition33%1/3
defined $virt{$key} and &is_real($virt{$key})
!ll&&!rl&&r
002
MC/DC0%0/2
defined $virt{$key} and &is_real($virt{$key})
defined $virt{$key} &is_real($virt{$key})
Truth table33%1/3
A and B
A= defined $virt{$key}B= &is_real($virt{$key})
ABresult
0X0
100
111
431 } elsif ($term eq 'whole') {
432 2 ▶ return unless defined $virt{$key} && is_whole($virt{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and &is_whole($virt{$key}) 1 1 2
Condition66%2/3
defined $virt{$key} and &is_whole($virt{$key})
!ll&&!rl&&r
011
MC/DC50%1/2
defined $virt{$key} and &is_whole($virt{$key})
defined $virt{$key} &is_whole($virt{$key})
Truth table66%2/3
A and B
A= defined $virt{$key}B= &is_whole($virt{$key})
ABresult
0X0
100
111
433 } elsif ($term eq 'string') {
434 2 ▶ return unless defined $virt{$key} && is_string($virt{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and &is_string($virt{$key}) 0 2 2
Condition33%1/3
defined $virt{$key} and &is_string($virt{$key})
!ll&&!rl&&r
002
MC/DC0%0/2
defined $virt{$key} and &is_string($virt{$key})
defined $virt{$key} &is_string($virt{$key})
Truth table33%1/3
A and B
A= defined $virt{$key}B= &is_string($virt{$key})
ABresult
0X0
100
111
435 } elsif ($term eq 'array') {
436 2 ▶ return unless defined $virt{$key} && ref $virt{$key} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and ref $virt{$key} eq "ARRAY" 0 2 2
Condition33%1/3
defined $virt{$key} and ref $virt{$key} eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/2
defined $virt{$key} and ref $virt{$key} eq "ARRAY"
defined $virt{$key} ref $virt{$key} eq "ARRAY"
Truth table33%1/3
A and B
A= defined $virt{$key}B= ref $virt{$key} eq "ARRAY"
ABresult
0X0
100
111
437 } elsif ($term eq 'hash') {
438 5 ▶ return unless defined $virt{$key} && ref $virt{$key} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and ref $virt{$key} eq "HASH" 1 4 5
Condition66%2/3
defined $virt{$key} and ref $virt{$key} eq "HASH"
!ll&&!rl&&r
014
MC/DC50%1/2
defined $virt{$key} and ref $virt{$key} eq "HASH"
defined $virt{$key} ref $virt{$key} eq "HASH"
Truth table66%2/3
A and B
A= defined $virt{$key}B= ref $virt{$key} eq "HASH"
ABresult
0X0
100
111
439 } elsif ($term eq 'bool') {
440 # boolean - not really supported, will always return true since everything in Perl is a boolean
441 } elsif ($term eq 'date') {
442 3 ▶ return unless defined $virt{$key} && !ref $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and not ref $virt{$key} 0 3 3
Condition33%1/3
defined $virt{$key} and not ref $virt{$key}
!ll&&!rl&&r
003
MC/DC0%0/2
defined $virt{$key} and not ref $virt{$key}
defined $virt{$key} not ref $virt{$key}
Truth table33%1/3
A and not(B)
A= defined $virt{$key}B= not ref $virt{$key}
ABresult
0X0
100
111
443 3 ▶ my $date = try { DateTime::Format::W3CDTF->parse_datetime($virt{$key}) } catch { undef };
Subroutine __ANON__: called CC=1 SCAR=0.0
444 3 ▶ return unless blessed $date && blessed $date eq 'DateTime';
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($date) and builtin::blessed($date) eq "DateTime" 1 2 3
Condition66%2/3
builtin::blessed($date) and builtin::blessed($date) eq "DateTime"
!ll&&!rl&&r
102
MC/DC50%1/2
builtin::blessed($date) and builtin::blessed($date) eq "DateTime"
builtin::blessed($date) builtin::blessed($date) eq "DateTime"
Truth table66%2/3
A and B
A= builtin::blessed($date)B= builtin::blessed($date) eq "DateTime"
ABresult
0X0
100
111
445 } elsif ($term eq 'null') {
446 3 ▶ return unless exists $virt{$key} && !defined $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $virt{$key} and not defined $virt{$key} 1 2 3
Condition66%2/3
exists $virt{$key} and not defined $virt{$key}
!ll&&!rl&&r
012
MC/DC50%1/2
exists $virt{$key} and not defined $virt{$key}
exists $virt{$key} not defined $virt{$key}
Truth table66%2/3
A and not(B)
A= exists $virt{$key}B= not defined $virt{$key}
ABresult
0X0
100
111
447 } elsif ($term eq 'regex') {
448 1 ▶ return unless defined $virt{$key} && ref $virt{$key} eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and ref $virt{$key} eq "Regexp" 0 1 1
Condition33%1/3
defined $virt{$key} and ref $virt{$key} eq "Regexp"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $virt{$key} and ref $virt{$key} eq "Regexp"
defined $virt{$key} ref $virt{$key} eq "Regexp"
Truth table33%1/3
A and B
A= defined $virt{$key}B= ref $virt{$key} eq "Regexp"
ABresult
0X0
100
111
449 }
450 }
451 }
452 }
453 } elsif (ref $value eq 'ARRAY') {
454 1 ▶ return unless Compare($value, $virt{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless Compare($value, $virt{$key}) 0 1 1
455 }
456  
457 84 return 1;
458 }
459  
460 ##############################################
461 # _array_has_eq( $value, \@array ) #
462 # ========================================== #
463 # $value - the value to check for #
464 # $array - the array to search in #
465 # ------------------------------------------ #
466 # returns true if the value exists in the #
467 # array provided. #
468 ##############################################
469  
470 sub _array_has_eq {
471 0 ▶ my ($value, $array) = @_;
Subroutine _array_has_eq: not called CC=3 SCAR=24.8
472  
473 0 foreach (@$array) {
474 0 ▶ return 1 if $_ eq $value;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $value 0 0 0
475 }
476  
477 0 return;
478 }
479  
480 ##############################################
481 # _array_has_re( $regex, \@array ) #
482 # ========================================== #
483 # $regex - the regex to check for #
484 # $array - the array to search in #
485 # ------------------------------------------ #
486 # returns true if a value exists in the #
487 # array provided that matches the regex. #
488 ##############################################
489  
490 sub _array_has_re {
491 0 ▶ my ($re, $array) = @_;
Subroutine _array_has_re: not called CC=3 SCAR=24.8
492  
493 0 foreach (@$array) {
494 0 ▶ return 1 if m/$re/;
Branch: 1 branch
BranchTrueFalseTotal
if /$re/ 0 0 0
495 }
496  
497 0 return;
498 }
499  
500 ##############################################
501 # _has_adv_que( \%hash ) #
502 # ========================================== #
503 # $hash - the hash-ref to search in #
504 # ------------------------------------------ #
505 # returns true if the hash-ref has any of #
506 # the lang's advanced query operators #
507 ##############################################
508  
509 sub _has_adv_que {
510 90 ▶ my $hash = shift;
Subroutine _has_adv_que: called CC=3 SCAR=11.0
511  
512 90 foreach ('$gt', '$gte', '$lt', '$lte', '$all', '$exists', '$mod', '$eq', '$ne', '$in', '$nin', '$size', '$type') {
513 676 ▶ return 1 if exists $hash->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{$_} 88 588 676
514 }
515  
516 2 return;
517 }
518  
519 ##############################################
520 # _value_in( $value, \@array ) #
521 # ========================================== #
522 # $value - the value to check for #
523 # $array - the array to search in #
524 # ------------------------------------------ #
525 # returns true if the value is one of the #
526 # values from the array. #
527 ##############################################
528  
529 sub _value_in {
530 13 ▶ my ($value, $array) = @_;
Subroutine _value_in: called CC=10 SCAR=24.1
531  
532 13 foreach (@$array) {
533 46 ▶ next if is_float($_) && !is_float($value);
Branch: 1 branch
BranchTrueFalseTotal
if &is_float($_) and not &is_float($value) 0 46 46
Condition66%2/3
&is_float($_) and not &is_float($value)
!ll&&!rl&&r
26200
MC/DC0%0/2
&is_float($_) and not &is_float($value)
&is_float($_) not &is_float($value)
Truth table66%2/3
A and not(B)
A= &is_float($_)B= not &is_float($value)
ABresult
0X0
100
111
534 46 ▶ next if !is_float($_) && is_float($value);
Branch: 1 branch
BranchTrueFalseTotal
if not &is_float($_) and &is_float($value) 0 46 46
Condition66%2/3
not &is_float($_) and &is_float($value)
!ll&&!rl&&r
20260
MC/DC0%0/2
not &is_float($_) and &is_float($value)
not &is_float($_) &is_float($value)
Truth table66%2/3
not(A) and B
A= not &is_float($_)B= &is_float($value)
ABresult
0X0
100
111
535 46 ▶ return 1 if is_float($_) && $value == $_;
Branch: 1 branch
BranchTrueFalseTotal
if &is_float($_) and $value == $_ 0 46 46
Condition66%2/3
&is_float($_) and $value == $_
!ll&&!rl&&r
26200
MC/DC0%0/2
&is_float($_) and $value == $_
&is_float($_) $value == $_
Truth table66%2/3
A and B
A= &is_float($_)B= $value == $_
ABresult
0X0
100
111
536 46 ▶ return 1 if !is_float($_) && $value eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if not &is_float($_) and $value eq $_ 9 37 46
Condition100%3/3
not &is_float($_) and $value eq $_
!ll&&!rl&&r
20179
MC/DC100%2/2
not &is_float($_) and $value eq $_
not &is_float($_) $value eq $_
Truth table100%3/3
not(A) and B
A= not &is_float($_)B= $value eq $_
ABresult
0X0
100
111
537 }
538  
539 4 return;
540 }
541  
542 =head2 update_doc( \%document, \%update )
543  
544 Receives a document hash-ref and an update hash-ref, and updates the
545 document in-place according to the update hash-ref. Also returns the document
546 after the update. If the update hash-ref doesn't have any of the update
547 modifiers described by the language, then the update hash-ref is considered
548 as what the document should now be, and so will simply replace the document
549 hash-ref (once again, in accordance with MongoDB).
550  
551 See L<MQUL::Reference/"UPDATE STRUCTURE"> to learn about the structure of
552 update hash-refs.
553  
554 =cut
555  
556 sub update_doc {
557 20 ▶ my ($doc, $obj) = @_;
Subroutine update_doc: called CC=84 SCAR=51.5
Pod documented
558  
559 20 ▶ croak "MQUL::update_doc() requires a document hash-ref."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc and ref $doc and ref $doc eq "HASH" 2 17 19
Condition100%6/6
defined $doc and ref $doc and ref $doc eq "HASH"
!ll&&!rl&&r
2117
defined $doc and ref $doc
!ll&&!rl&&r
1118
MC/DC100%3/3
defined $doc and ref $doc and ref $doc eq "HASH"
defined $doc ref $doc ref $doc eq "HASH"
Truth table100%4/4
A and B and C
A= defined $docB= ref $docC= ref $doc eq "HASH"
ABCresult
0XX0
10X0
1100
1111
560 unless defined $doc && ref $doc && ref $doc eq 'HASH';
561 17 ▶ croak "MQUL::update_doc() requires an update hash-ref."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj and ref $obj and ref $obj eq "HASH" 1 15 16
Condition83%5/6
defined $obj and ref $obj and ref $obj eq "HASH"
!ll&&!rl&&r
1115
defined $obj and ref $obj
!ll&&!rl&&r
1016
MC/DC66%2/3
defined $obj and ref $obj and ref $obj eq "HASH"
defined $obj ref $obj ref $obj eq "HASH"
Truth table75%3/4
A and B and C
A= defined $objB= ref $objC= ref $obj eq "HASH"
ABCresult
0XX0
10X0
1100
1111
562 unless defined $obj && ref $obj && ref $obj eq 'HASH';
563  
564 # we only need to do something if the $obj hash-ref has any advanced
565 # update operations, otherwise $obj is meant to be the new $doc
566  
567 15 ▶ if (&_has_adv_upd($obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_has_adv_upd($obj)) { } 14 1 15
568 14 foreach my $op (keys %$obj) {
569 18 ▶ if ($op eq '$inc') {
Branch: 12 branches
BranchTrueFalseTotal
if ($op eq '$inc') { } 2 16 18
elsif ($op eq '$set') { } 2 14 16
elsif ($op eq '$unset') { } 2 12 14
elsif ($op eq '$rename') { } 1 11 12
elsif ($op eq '$push') { } 1 10 11
elsif ($op eq '$pushAll') { } 1 9 10
elsif ($op eq '$addToSet') { } 2 7 9
elsif ($op eq '$pop') { } 2 5 7
elsif ($op eq '$shift') { } 1 4 5
elsif ($op eq '$splice') { } 1 3 4
elsif ($op eq '$pull') { } 2 1 3
elsif ($op eq '$pullAll') { } 1 0 1
570 # increase numerically
571 2 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 2 2
572 2 foreach my $field (keys %{$obj->{$op}}) {
573 2 ▶ $doc->{$field} ||= 0;
Condition50%1/2
$doc->{$field} ||= 0
l!l
20
MC/DC0%0/1
$doc->{$field} ||= 0
$doc->{$field}
Truth table50%1/2
A
A= $doc->{$field}
Aresult
11
00
574 2 $doc->{$field} += $obj->{$op}->{$field};
575 }
576 } elsif ($op eq '$set') {
577 # set key-value pairs
578 2 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 2 2
579 2 foreach my $field (keys %{$obj->{$op}}) {
580 3 $doc->{$field} = $obj->{$op}->{$field};
581 }
582 } elsif ($op eq '$unset') {
583 # remove key-value pairs
584 2 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 2 2
585 2 foreach my $field (keys %{$obj->{$op}}) {
586 2 ▶ delete $doc->{$field} if $obj->{$op}->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{$op}{$field} 2 0 2
587 }
588 } elsif ($op eq '$rename') {
589 # rename attributes
590 1 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 1 1
591 1 foreach my $field (keys %{$obj->{$op}}) {
592 $doc->{$obj->{$op}->{$field}} = delete $doc->{$field}
593 1 ▶ if exists $doc->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if exists $doc->{$field} 1 0 1
594 }
595 } elsif ($op eq '$push') {
596 # push values to end of arrays
597 1 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 1 1
598 1 foreach my $field (keys %{$obj->{$op}}) {
599 croak "The $field attribute is not an array in the doc."
600 1 ▶ if defined $doc->{$field} && ref $doc->{$field} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc->{$field} and ref $doc->{$field} ne "ARRAY" 0 1 1
Condition33%1/3
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
!ll&&!rl&&r
010
MC/DC0%0/2
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
defined $doc->{$field} ref $doc->{$field} ne "ARRAY"
Truth table33%1/3
A and B
A= defined $doc->{$field}B= ref $doc->{$field} ne "ARRAY"
ABresult
0X0
100
111
601 1 ▶ $doc->{$field} ||= [];
Condition50%1/2
$doc->{$field} ||= []
l!l
10
MC/DC0%0/1
$doc->{$field} ||= []
$doc->{$field}
Truth table50%1/2
A
A= $doc->{$field}
Aresult
11
00
602 1 push(@{$doc->{$field}}, $obj->{$op}->{$field});
603 }
604 } elsif ($op eq '$pushAll') {
605 # push a list of values to end of arrays
606 1 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 1 1
607 1 foreach my $field (keys %{$obj->{$op}}) {
608 croak "The $field attribute is not an array in the doc."
609 1 ▶ if defined $doc->{$field} && ref $doc->{$field} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc->{$field} and ref $doc->{$field} ne "ARRAY" 0 1 1
Condition33%1/3
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
!ll&&!rl&&r
010
MC/DC0%0/2
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
defined $doc->{$field} ref $doc->{$field} ne "ARRAY"
Truth table33%1/3
A and B
A= defined $doc->{$field}B= ref $doc->{$field} ne "ARRAY"
ABresult
0X0
100
111
610 1 ▶ $doc->{$field} ||= [];
Condition50%1/2
$doc->{$field} ||= []
l!l
10
MC/DC0%0/1
$doc->{$field} ||= []
$doc->{$field}
Truth table50%1/2
A
A= $doc->{$field}
Aresult
11
00
611 1 push(@{$doc->{$field}}, @{$obj->{$op}->{$field}});
612 }
613 } elsif ($op eq '$addToSet') {
614 # push values to arrays only if they're not already there
615 2 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 2 2
616 2 foreach my $field (keys %{$obj->{$op}}) {
617 croak "The $field attribute is not an array in the doc."
618 2 ▶ if defined $doc->{$field} && ref $doc->{$field} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc->{$field} and ref $doc->{$field} ne "ARRAY" 0 2 2
Condition33%1/3
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
!ll&&!rl&&r
020
MC/DC0%0/2
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
defined $doc->{$field} ref $doc->{$field} ne "ARRAY"
Truth table33%1/3
A and B
A= defined $doc->{$field}B= ref $doc->{$field} ne "ARRAY"
ABresult
0X0
100
111
619 2 ▶ $doc->{$field} ||= [];
Condition50%1/2
$doc->{$field} ||= []
l!l
20
MC/DC0%0/1
$doc->{$field} ||= []
$doc->{$field}
Truth table50%1/2
A
A= $doc->{$field}
Aresult
11
00
620 2 ▶ my @add = ref $obj->{$op}->{$field} && ref $obj->{$op}->{$field} eq 'ARRAY' ? @{$obj->{$op}->{$field}} : ($obj->{$op}->{$field});
Branch: 1 branch
BranchTrueFalseTotal
ref $obj->{$op}{$field} && ref $obj->{$op}{$field} eq 'ARRAY' ? : 1 1 2
Condition66%2/3
ref $obj->{$op}{$field} and ref $obj->{$op}{$field} eq "ARRAY"
!ll&&!rl&&r
101
MC/DC50%1/2
ref $obj->{$op}{$field} and ref $obj->{$op}{$field} eq "ARRAY"
ref $obj->{$op}{$field} ref $obj->{$op}{$field} eq "ARRAY"
Truth table66%2/3
A and B
A= ref $obj->{$op}{$field}B= ref $obj->{$op}{$field} eq "ARRAY"
ABresult
0X0
100
111
621 2 foreach my $val (@add) {
622 2 push(@{$doc->{$field}}, $val)
623 4 ▶ unless defined &_index_of($val, $doc->{$field});
Branch: 1 branch
BranchTrueFalseTotal
unless defined &_index_of($val, $doc->{$field}) 2 2 4
624 }
625 }
626 } elsif ($op eq '$pop') {
627 # pop the last item from an array
628 2 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 2 2
629 2 foreach my $field (keys %{$obj->{$op}}) {
630 croak "The $field attribute is not an array in the doc."
631 2 ▶ if defined $doc->{$field} && ref $doc->{$field} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc->{$field} and ref $doc->{$field} ne "ARRAY" 0 2 2
Condition33%1/3
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
!ll&&!rl&&r
020
MC/DC0%0/2
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
defined $doc->{$field} ref $doc->{$field} ne "ARRAY"
Truth table33%1/3
A and B
A= defined $doc->{$field}B= ref $doc->{$field} ne "ARRAY"
ABresult
0X0
100
111
632 2 ▶ $doc->{$field} ||= [];
Condition50%1/2
$doc->{$field} ||= []
l!l
20
MC/DC0%0/1
$doc->{$field} ||= []
$doc->{$field}
Truth table50%1/2
A
A= $doc->{$field}
Aresult
11
00
633 2 pop(@{$doc->{$field}})
634 2 ▶ if $obj->{$op}->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{$op}{$field} 2 0 2
635 }
636 } elsif ($op eq '$shift') {
637 # shift the first item from an array
638 1 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 1 1
639 1 foreach my $field (keys %{$obj->{$op}}) {
640 croak "The $field attribute is not an array in the doc."
641 1 ▶ if defined $doc->{$field} && ref $doc->{$field} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc->{$field} and ref $doc->{$field} ne "ARRAY" 0 1 1
Condition33%1/3
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
!ll&&!rl&&r
010
MC/DC0%0/2
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
defined $doc->{$field} ref $doc->{$field} ne "ARRAY"
Truth table33%1/3
A and B
A= defined $doc->{$field}B= ref $doc->{$field} ne "ARRAY"
ABresult
0X0
100
111
642 1 ▶ $doc->{$field} ||= [];
Condition50%1/2
$doc->{$field} ||= []
l!l
10
MC/DC0%0/1
$doc->{$field} ||= []
$doc->{$field}
Truth table50%1/2
A
A= $doc->{$field}
Aresult
11
00
643 1 shift(@{$doc->{$field}})
644 1 ▶ if $obj->{$op}->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{$op}{$field} 1 0 1
645 }
646 } elsif ($op eq '$splice') {
647 # splice offsets from arrays
648 1 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 1 1
649 1 foreach my $field (keys %{$obj->{$op}}) {
650 croak "The $field attribute is not an array in the doc."
651 1 ▶ if defined $doc->{$field} && ref $doc->{$field} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc->{$field} and ref $doc->{$field} ne "ARRAY" 0 1 1
Condition33%1/3
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
!ll&&!rl&&r
010
MC/DC0%0/2
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
defined $doc->{$field} ref $doc->{$field} ne "ARRAY"
Truth table33%1/3
A and B
A= defined $doc->{$field}B= ref $doc->{$field} ne "ARRAY"
ABresult
0X0
100
111
652 next unless ref $obj->{$op}->{$field} &&
653 ref $obj->{$op}->{$field} eq 'ARRAY' &&
654 1 ▶ scalar @{$obj->{$op}->{$field}} == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op}{$field} and ref $obj->{$op}{$field} eq "ARRAY" and scalar @{$obj->{$op}{$field};} == 2 0 1 1
Condition33%2/6
ref $obj->{$op}{$field} and ref $obj->{$op}{$field} eq "ARRAY" and scalar @{$obj->{$op}{$field};} == 2
!ll&&!rl&&r
001
ref $obj->{$op}{$field} and ref $obj->{$op}{$field} eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/3
ref $obj->{$op}{$field} and ref $obj->{$op}{$field} eq "ARRAY" and scalar @{$obj->{$op}{$field};} == 2
ref $obj->{$op}{$field} ref $obj->{$op}{$field} eq "ARRAY" scalar @{$obj->{$op}{$field};} == 2
Truth table25%1/4
A and B and C
A= ref $obj->{$op}{$field}B= ref $obj->{$op}{$field} eq "ARRAY"C= scalar @{$obj->{$op}{$field};} == 2
ABCresult
0XX0
10X0
1100
1111
655 1 ▶ $doc->{$field} ||= [];
Condition50%1/2
$doc->{$field} ||= []
l!l
10
MC/DC0%0/1
$doc->{$field} ||= []
$doc->{$field}
Truth table50%1/2
A
A= $doc->{$field}
Aresult
11
00
656 1 splice(@{$doc->{$field}}, $obj->{$op}->{$field}->[0], $obj->{$op}->{$field}->[1]);
657 }
658 } elsif ($op eq '$pull') {
659 # remove values from arrays
660 2 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 2 2
661 2 foreach my $field (keys %{$obj->{$op}}) {
662 croak "The $field attribute is not an array in the doc."
663 2 ▶ if defined $doc->{$field} && ref $doc->{$field} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc->{$field} and ref $doc->{$field} ne "ARRAY" 0 2 2
Condition33%1/3
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
!ll&&!rl&&r
020
MC/DC0%0/2
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
defined $doc->{$field} ref $doc->{$field} ne "ARRAY"
Truth table33%1/3
A and B
A= defined $doc->{$field}B= ref $doc->{$field} ne "ARRAY"
ABresult
0X0
100
111
664 2 ▶ $doc->{$field} ||= [];
Condition50%1/2
$doc->{$field} ||= []
l!l
20
MC/DC0%0/1
$doc->{$field} ||= []
$doc->{$field}
Truth table50%1/2
A
A= $doc->{$field}
Aresult
11
00
665 2 my $i = &_index_of($obj->{$op}->{$field}, $doc->{$field});
666 2 ▶ while (defined $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i) 2 2 4
667 2 splice(@{$doc->{$field}}, $i, 1);
668 2 $i = &_index_of($obj->{$op}->{$field}, $doc->{$field});
669 }
670 }
671 } elsif ($op eq '$pullAll') {
672 # remove a list of values from arrays
673 1 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 1 1
674 1 foreach my $field (keys %{$obj->{$op}}) {
675 croak "The $field attribute is not an array in the doc."
676 1 ▶ if defined $doc->{$field} && ref $doc->{$field} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc->{$field} and ref $doc->{$field} ne "ARRAY" 0 1 1
Condition33%1/3
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
!ll&&!rl&&r
010
MC/DC0%0/2
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
defined $doc->{$field} ref $doc->{$field} ne "ARRAY"
Truth table33%1/3
A and B
A= defined $doc->{$field}B= ref $doc->{$field} ne "ARRAY"
ABresult
0X0
100
111
677 1 ▶ $doc->{$field} ||= [];
Condition50%1/2
$doc->{$field} ||= []
l!l
10
MC/DC0%0/1
$doc->{$field} ||= []
$doc->{$field}
Truth table50%1/2
A
A= $doc->{$field}
Aresult
11
00
678 1 foreach my $value (@{$obj->{$op}->{$field}}) {
679 2 my $i = &_index_of($value, $doc->{$field});
680 2 ▶ while (defined $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i) 2 2 4
681 2 splice(@{$doc->{$field}}, $i, 1);
682 2 $i = &_index_of($value, $doc->{$field});
683 }
684 }
685 }
686 }
687 }
688 } else {
689 # $obj is actually the new $doc
690 1 %$doc = %$obj;
691 }
692  
693 15 return $doc;
694 }
695  
696 ##############################################
697 # _has_adv_upd( \%hash ) #
698 # ========================================== #
699 # $hash - the hash-ref to search in #
700 # ------------------------------------------ #
701 # returns true if the hash-ref has any of #
702 # the lang's advanced update operators #
703 ##############################################
704  
705 sub _has_adv_upd {
706 15 ▶ my $hash = shift;
Subroutine _has_adv_upd: called CC=3 SCAR=11.0
707  
708 15 foreach ('$inc', '$set', '$unset', '$push', '$pushAll', '$addToSet', '$pop', '$shift', '$splice', '$pull', '$pullAll', '$rename', '$bit') {
709 98 ▶ return 1 if exists $hash->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{$_} 14 84 98
710 }
711  
712 1 return;
713 }
714  
715 ##############################################
716 # _index_of( $value, \@array ) #
717 # ========================================== #
718 # $value - the value to search for #
719 # $array - the array to search in #
720 # ------------------------------------------ #
721 # searches for the provided value in the #
722 # array, and returns its index if it is #
723 # found, or undef otherwise. #
724 ##############################################
725  
726 sub _index_of {
727 12 ▶ my ($value, $array) = @_;
Subroutine _index_of: called CC=6 SCAR=20.3
728  
729 12 ▶ for (my $i = 0; $i < scalar @$array; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$array) 23 6 29
730 23 ▶ if (is_float($array->[$i]) && is_float($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_float($array->[$i]) and &is_float($value)) { } 0 23 23
Condition33%1/3
&is_float($array->[$i]) and &is_float($value)
!ll&&!rl&&r
2300
MC/DC0%0/2
&is_float($array->[$i]) and &is_float($value)
&is_float($array->[$i]) &is_float($value)
Truth table33%1/3
A and B
A= &is_float($array->[$i])B= &is_float($value)
ABresult
0X0
100
111
731 0 ▶ return $i if $array->[$i] == $value;
Branch: 1 branch
BranchTrueFalseTotal
if $array->[$i] == $value 0 0 0
732 } else {
733 23 ▶ return $i if $array->[$i] eq $value;
Branch: 1 branch
BranchTrueFalseTotal
if $array->[$i] eq $value 6 17 23
734 }
735 }
736  
737 6 return;
738 }
739  
740 ##############################################
741 # _parse_function( $doc, $key ) #
742 # ========================================== #
743 # $doc - the document #
744 # $key - the key referencing a function and #
745 # a list of attributes, such as #
746 # min(attr1, attr2, attr3) #
747 # ------------------------------------------ #
748 # calculates the value using the appropriate #
749 # function and returns the result #
750 ##############################################
751  
752 sub _parse_function {
753 23 ▶ my ($doc, $def) = @_;
Subroutine _parse_function: called CC=6 SCAR=17.9
754  
755 23 my ($func) = keys %$def;
756  
757 die "Unrecognized function $func"
758 23 ▶ unless exists $BUILTINS{$func};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $MQUL::BUILTINS{$func} 0 23 23
759  
760 $def->{$func} = [$def->{$func}]
761 23 ▶ unless ref $def->{$func};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $def->{$func} 4 19 23
762  
763 23 my @vals;
764 23 foreach (@{$def->{$func}}) {
765 53 my ($v, $k) = _expand_dot_notation($doc, $_);
766 53 ▶ push(@vals, $v)
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 49 4 53
767 if defined $v;
768 }
769  
770 23 ▶ return unless scalar @vals;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @vals 2 21 23
771  
772 21 return $BUILTINS{$func}->(@vals);
773 }
774  
775 ##############################################
776 # _expand_dot_notation( $doc, $key ) #
777 # ========================================== #
778 # $doc - the document #
779 # $key - the key using dot notation #
780 # ------------------------------------------ #
781 # takes a key using the dot notation, and #
782 # returns the value of the document at the #
783 # end of the chain (if any), plus the key at #
784 # the end of the chain. #
785 ##############################################
786  
787 sub _expand_dot_notation {
788 70 ▶ my ($doc, $key) = @_;
Subroutine _expand_dot_notation: called CC=10 SCAR=23.0
789  
790 70 ▶ return ($doc->{$key}, $key)
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /\./ 42 28 70
791 unless $key =~ m/\./;
792  
793 28 my @way_there = split(/\./, $key);
794  
795 28 $key = shift @way_there;
796 28 my %virt = ( $key => $doc->{$key} );
797  
798 28 ▶ while (scalar @way_there) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @way_there) 50 28 78
799 50 $key = shift @way_there;
800 50 my ($have) = values %virt;
801  
802 50 ▶ if ($have && ref $have eq 'HASH' && exists $have->{$key}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($have and ref $have eq 'HASH' and exists $have->{$key}) { } 35 15 50
elsif ($have and ref $have eq 'ARRAY' and $key =~ /^\d+$/ and scalar @$have > $key) { } 10 5 15
Condition100%6/6
$have and ref $have eq "HASH" and exists $have->{$key}
!ll&&!rl&&r
13235
$have and ref $have eq "HASH"
!ll&&!rl&&r
11237
MC/DC100%3/3
$have and ref $have eq "HASH" and exists $have->{$key}
$have ref $have eq "HASH" exists $have->{$key}
Truth table100%4/4
A and B and C
A= $haveB= ref $have eq "HASH"C= exists $have->{$key}
ABCresult
0XX0
10X0
1100
1111
803 35 %virt = ( $key => $have->{$key} );
804 ▶ } elsif ($have && ref $have eq 'ARRAY' && $key =~ m/^\d+$/ && scalar @$have > $key) {
Condition77%7/9
$have and ref $have eq "ARRAY" and $key =~ /^\d+$/ and scalar @$have > $key
!ll&&!rl&&r
5010
$have and ref $have eq "ARRAY" and $key =~ /^\d+$/
!ll&&!rl&&r
5010
$have and ref $have eq "ARRAY"
!ll&&!rl&&r
1410
MC/DC50%2/4
$have and ref $have eq "ARRAY" and $key =~ /^\d+$/ and scalar @$have > $key
$have ref $have eq "ARRAY" $key =~ /^\d+$/ scalar @$have > $key
Truth table60%3/5
A and B and C and D
A= $haveB= ref $have eq "ARRAY"C= $key =~ /^\d+$/D= scalar @$have > $key
ABCDresult
0XXX0
10XX0
110X0
11100
11111
805 10 %virt = ( $key => $have->[$key] )
806 } else {
807 5 %virt = ();
808 }
809 }
810  
811 28 return ($virt{$key}, $key);
812 }
813  
814 =head1 DIAGNOSTICS
815  
816 =over
817  
818 =item C<< MQUL::doc_matches() requires a document hash-ref. >>
819  
820 This error means that you've either haven't passed the C<doc_matches()>
821 subroutine any parameters, or given it a non-hash-ref document.
822  
823 =item C<< MQUL::doc_matches() expects a query hash-ref. >>
824  
825 This error means that you've passed the C<doc_matches()> attribute a
826 non-hash-ref query variable. While you don't actually have to pass a
827 query variable, if you do, it has to be a hash-ref.
828  
829 =item C<< MQUL::update_doc() requires a document hash-ref. >>
830  
831 This error means that you've either haven't passed the C<update_doc()>
832 subroutine any parameters, or given it a non-hash-ref document.
833  
834 =item C<< MQUL::update_doc() requires an update hash-ref. >>
835  
836 This error means that you've passed the C<update_doc()> subroutine a
837 non-hash-ref update variable.
838  
839 =item C<< The %s attribute is not an array in the doc. >>
840  
841 This error means that your update hash-ref tries to modify an array attribute
842 (with C<$push>, C<$pushAll>, C<$addToSet>, C<$pull>, C<$pullAll>,
843 C<$pop>, C<$shift> and C<$splice>), but the attribute in the document
844 provided to the C<update_doc()> subroutine is not an array.
845  
846 =back
847  
848 =head1 CONFIGURATION AND ENVIRONMENT
849
850 MQUL requires no configuration files or environment variables.
851  
852 =head1 DEPENDENCIES
853  
854 MQUL depends on the following modules:
855  
856 =over
857  
858 =item * L<Data::Compare>
859  
860 =item * L<Data::Types>
861  
862 =item * L<DateTime::Format::W3CDTF>
863  
864 =item * L<Scalar::Util>
865  
866 =item * L<Try::Tiny>
867  
868 =back
869  
870 =head1 INCOMPATIBILITIES
871  
872 None reported.
873  
874 =head1 BUGS AND LIMITATIONS
875  
876 No bugs have been reported.
877  
878 Please report any bugs or feature requests to
879 C<bug-MQUL@rt.cpan.org>, or through the web interface at
880 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MQUL>.
881  
882 =head1 AUTHOR
883  
884 Ido Perlmuter <ido at ido50 dot net>
885  
886 =head1 LICENSE AND COPYRIGHT
887  
888 Copyright (c) 2011-2015, Ido Perlmuter C<< ido at ido50 dot net >>.
889  
890 This module is free software; you can redistribute it and/or
891 modify it under the same terms as Perl itself, either version
892 5.8.1 or any later version. See L<perlartistic|perlartistic>
893 and L<perlgpl|perlgpl>.
894  
895 The full text of the license can be found in the
896 LICENSE file included with this module.
897  
898 =head1 DISCLAIMER OF WARRANTY
899  
900 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
901 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
902 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
903 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
904 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
905 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
906 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
907 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
908 NECESSARY SERVICING, REPAIR, OR CORRECTION.
909  
910 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
911 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
912 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
913 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
914 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
915 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
916 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
917 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
918 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
919 SUCH DAMAGES.
920  
921 =cut
922  
923 3 1;
924 __END__
↑ Summary