blib/lib/MQUL.pm

filter: Statementstmt 93.3% 294 / 315 Branchbran 73.7% 255 / 346 Conditioncond 56.6% 132 / 233 MC/DCmcdc 25.1% 34 / 135 Subroutinesub 93.1% 27 / 29 Podpod 100% 2 / 2 totaltotal 70.1% 744 / 1060 SCAR 72.4
CC
287
Cov
76%
_attribute_matches
674.9
update_doc
171.8
doc_matches
28.9
CRAP 1401.0
↑ 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.001000";
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 109 ▶ my ($doc, $query, $defs) = @_;
Subroutine doc_matches: called CC=28 SCAR=33.6
Pod documented
237  
238 109 ▶ croak 'MQUL::doc_matches() requires a document hash-ref.'
Branch: 1 branch
BranchTrueFalseTotal
unless $doc and ref $doc and ref $doc eq "HASH" 2 106 108
Condition100%6/6
$doc and ref $doc and ref $doc eq "HASH"
!ll&&!rl&&r
21106
$doc and ref $doc
!ll&&!rl&&r
11107
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 106 ▶ croak 'MQUL::doc_matches() expects a query hash-ref.'
Branch: 1 branch
BranchTrueFalseTotal
if $query and !ref($query) || ref $query ne "HASH" 2 104 106
Condition100%6/6
$query and !ref($query) || ref $query ne "HASH"
!ll&&!rl&&r
11032
not ref $query or ref $query ne "HASH"
l!l&&r!l&&!r
11103
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 104 ▶ 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 104 104
Condition44%4/9
$defs and !ref($defs) || ref $defs ne "ARRAY" || scalar @$defs % 2 != 0
!ll&&!rl&&r
85190
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 104 ▶ $query ||= {};
Condition100%2/2
$query ||= {}
l!l
1031
MC/DC100%1/1
$query ||= {}
$query
Truth table100%2/2
A
A= $query
Aresult
11
00
246  
247 104 ▶ if ($defs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defs) 19 85 104
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 104 foreach my $key (keys %$query) {
256 120 my $value = $query->{$key};
257 120 ▶ if ($key eq '$or' && ref $value eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq '$or' and ref $value eq 'ARRAY') { } 12 108 120
elsif ($key eq '$and' and ref $value eq 'ARRAY') { } 3 105 108
Condition66%2/3
$key eq "\$or" and ref $value eq "ARRAY"
!ll&&!rl&&r
108012
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 12 my $found;
259 12 foreach (@$value) {
260 20 ▶ next unless ref $_ eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "HASH" 0 20 20
261 20 my $ok = 1;
262  
263 20 ▶ while (my ($k, $v) = each %$_) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$_) 21 8 29
264 21 ▶ unless (&_attribute_matches($doc, $k, $v)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_attribute_matches($doc, $k, $v)) 12 9 21
265 12 undef $ok;
266 12 last;
267 }
268 }
269  
270 20 ▶ if ($ok) { # document matches this criteria
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 8 12 20
271 8 $found = 1;
272 8 last;
273 }
274 }
275 12 ▶ return unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 4 8 12
276 ▶ } elsif ($key eq '$and' && ref $value eq 'ARRAY') {
Condition66%2/3
$key eq "\$and" and ref $value eq "ARRAY"
!ll&&!rl&&r
10503
MC/DC50%1/2
$key eq "\$and" and ref $value eq "ARRAY"
$key eq "\$and" ref $value eq "ARRAY"
Truth table66%2/3
A and B
A= $key eq "\$and"B= ref $value eq "ARRAY"
ABresult
0X0
100
111
277 3 foreach (@$value) {
278 6 ▶ return unless &doc_matches($doc, $_, $defs);
Branch: 1 branch
BranchTrueFalseTotal
unless &doc_matches($doc, $_, $defs) 1 5 6
279 }
280 } else {
281 105 ▶ return unless &_attribute_matches($doc, $key, $value);
Branch: 1 branch
BranchTrueFalseTotal
unless &_attribute_matches($doc, $key, $value) 26 79 105
282 }
283 }
284  
285 # if we've reached here, the document matches, so return true
286 73 return 1;
287 }
288  
289 ##############################################
290 # _attribute_matches( $doc, $key, $value ) #
291 # ========================================== #
292 # $doc - the document hash-ref #
293 # $key - the attribute being checked #
294 # $value - the constraint for the attribute #
295 # taken from the query hash-ref #
296 # ------------------------------------------ #
297 # returns true if constraint is met in the #
298 # provided document. #
299 ##############################################
300  
301 3 my $funcs = join('|', keys %BUILTINS);
302  
303 sub _attribute_matches {
304 126 ▶ my ($doc, $key, $value) = @_;
Subroutine _attribute_matches: called CC=132 SCAR=65.1
305  
306 126 my %virt;
307 126 ▶ if ($key =~ m/\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\./) { } 17 109 126
308 # support for the dot notation
309 17 my ($v, $k) = _expand_dot_notation($doc, $key);
310  
311 17 $key = $k;
312 17 ▶ $virt{$key} = $v
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 13 4 17
313 if defined $v;
314 } else {
315 $virt{$key} = $doc->{$key}
316 109 ▶ if exists $doc->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $doc->{$key} 103 6 109
317 }
318  
319 126 ▶ if (!ref $value) { # if value is a scalar, we need to check for equality
Branch: 5 branches
BranchTrueFalseTotal
if (not ref $value) { } 31 95 126
elsif (builtin::blessed($value) and builtin::blessed($value) eq 'MongoDB::OID' || builtin::blessed($value) eq 'MorboDB::OID') { } 0 95 95
elsif (ref $value eq 'Regexp') { } 2 93 95
elsif (ref $value eq 'HASH') { } 92 1 93
elsif (ref $value eq 'ARRAY') { } 1 0 1
320 # (or, if the attribute is an array in the document,
321 # we need to check the value exists in it)
322 31 ▶ return unless defined $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} 2 29 31
323 29 ▶ if (ref $virt{$key} eq 'ARRAY') { # check the array has the requested value
Branch: 2 branches
BranchTrueFalseTotal
if (ref $virt{$key} eq 'ARRAY') { } 0 29 29
elsif (not ref $virt{$key}) { } 29 0 29
324 0 ▶ return unless &_array_has_eq($value, $virt{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless &_array_has_eq($value, $virt{$key}) 0 0 0
325 } elsif (!ref $virt{$key}) { # check the values are equal
326 29 ▶ return unless $virt{$key} eq $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} eq $value 9 20 29
327 } else { # we can't compare a non-scalar to a scalar, so return false
328 0 return;
329 }
330 ▶ } 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
9320
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
331 # we're trying to compare MongoDB::OIDs/MorboDB::OIDs
332 # (MorboDB is my in-memory clone of MongoDB)
333 0 ▶ return unless defined $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} 0 0 0
334 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
335 0 ▶ return unless $virt{$key}->value eq $value->value;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key}->value eq $value->value 0 0 0
336 } else {
337 0 return;
338 }
339 } elsif (ref $value eq 'Regexp') { # if the value is a regex, we need to check
340 # for a match (or, if the attribute is an array
341 # in the document, we need to check at least one
342 # value in it matches it)
343 2 ▶ return unless defined $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} 0 2 2
344 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
345 0 ▶ return unless &_array_has_re($value, $virt{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless &_array_has_re($value, $virt{$key}) 0 0 0
346 } elsif (!ref $virt{$key}) { # check the values match
347 2 ▶ return unless $virt{$key} =~ $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} =~ /$value/ 1 1 2
348 } else { # we can't compare a non-scalar to a scalar, so return false
349 0 return;
350 }
351 } elsif (ref $value eq 'HASH') { # if the value is a hash, than it either contains
352 # advanced queries, or it's just a hash that we
353 # want the document to have as-is
354 92 ▶ unless (&_has_adv_que($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_has_adv_que($value)) { } 90 2 92
355 # value hash-ref doesn't have any advanced
356 # queries, we need to check our document
357 # has an attributes with exactly the same hash-ref
358 # (and name of course)
359 2 ▶ return unless Compare($value, $virt{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless Compare($value, $virt{$key}) 0 2 2
360 } else {
361 # value contains advanced queries,
362 # we need to make sure our document has an
363 # attribute with the same name that matches
364 # all these queries
365 90 foreach my $q (keys %$value) {
366 99 my $term = $value->{$q};
367 99 ▶ if ($q eq '$gt') {
Branch: 13 branches
BranchTrueFalseTotal
if ($q eq '$gt') { } 14 85 99
elsif ($q eq '$gte') { } 7 78 85
elsif ($q eq '$lt') { } 8 70 78
elsif ($q eq '$lte') { } 6 64 70
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
368 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
369 14 ▶ if (is_float($virt{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_float($virt{$key})) { } 14 0 14
370 14 ▶ return unless $virt{$key} > $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} > $term 7 7 14
371 } else {
372 0 ▶ return unless $virt{$key} gt $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} gt $term 0 0 0
373 }
374 } elsif ($q eq '$gte') {
375 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
376 7 ▶ if (is_float($virt{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_float($virt{$key})) { } 7 0 7
377 7 ▶ return unless $virt{$key} >= $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} >= $term 1 6 7
378 } else {
379 0 ▶ return unless $virt{$key} ge $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} ge $term 0 0 0
380 }
381 } elsif ($q eq '$lt') {
382 8 ▶ return unless defined $virt{$key} && !ref $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and not ref $virt{$key} 0 8 8
Condition33%1/3
defined $virt{$key} and not ref $virt{$key}
!ll&&!rl&&r
008
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
383 8 ▶ if (is_float($virt{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_float($virt{$key})) { } 7 1 8
384 7 ▶ return unless $virt{$key} < $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} < $term 1 6 7
385 } else {
386 1 ▶ return unless $virt{$key} lt $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} lt $term 0 1 1
387 }
388 } elsif ($q eq '$lte') {
389 6 ▶ return unless defined $virt{$key} && !ref $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $virt{$key} and not ref $virt{$key} 0 6 6
Condition33%1/3
defined $virt{$key} and not ref $virt{$key}
!ll&&!rl&&r
006
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
390 6 ▶ if (is_float($virt{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_float($virt{$key})) { } 6 0 6
391 6 ▶ return unless $virt{$key} <= $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} <= $term 1 5 6
392 } else {
393 0 ▶ return unless $virt{$key} le $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} le $term 0 0 0
394 }
395 } elsif ($q eq '$eq') {
396 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
397 1 ▶ if (is_float($virt{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_float($virt{$key})) { } 0 1 1
398 0 ▶ return unless $virt{$key} == $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} == $term 0 0 0
399 } else {
400 1 ▶ return unless $virt{$key} eq $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} eq $term 0 1 1
401 }
402 } elsif ($q eq '$ne') {
403 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
404 2 ▶ if (is_float($virt{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_float($virt{$key})) { } 1 1 2
405 1 ▶ return unless $virt{$key} != $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} != $term 0 1 1
406 } else {
407 1 ▶ return unless $virt{$key} ne $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $virt{$key} ne $term 0 1 1
408 }
409 } elsif ($q eq '$exists') {
410 15 ▶ if ($term) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term) { } 10 5 15
411 10 ▶ return unless exists $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $virt{$key} 5 5 10
412 } else {
413 5 ▶ return if exists $virt{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $virt{$key} 1 4 5
414 }
415 ▶ } 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
416 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
417 ▶ } 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
418 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
419 ▶ } 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
420 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
421 ▶ } 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
422 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
423 ▶ } 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
424 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
425 3 foreach (@$term) {
426 6 ▶ return unless &_value_in($_, $virt{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless &_value_in($_, $virt{$key}) 1 5 6
427 }
428 ▶ } 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
429 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
430 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
431 } elsif ($term eq 'float') {
432 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
433 } elsif ($term eq 'real') {
434 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
435 } elsif ($term eq 'whole') {
436 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
437 } elsif ($term eq 'string') {
438 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
439 } elsif ($term eq 'array') {
440 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
441 } elsif ($term eq 'hash') {
442 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
443 } elsif ($term eq 'bool') {
444 # boolean - not really supported, will always return true since everything in Perl is a boolean
445 } elsif ($term eq 'date') {
446 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
447 3 ▶ my $date = try { DateTime::Format::W3CDTF->parse_datetime($virt{$key}) } catch { undef };
Subroutine __ANON__: called CC=1 SCAR=0.0
448 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
449 } elsif ($term eq 'null') {
450 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
451 } elsif ($term eq 'regex') {
452 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
453 }
454 }
455 }
456 }
457 } elsif (ref $value eq 'ARRAY') {
458 1 ▶ return unless Compare($value, $virt{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless Compare($value, $virt{$key}) 0 1 1
459 }
460  
461 88 return 1;
462 }
463  
464 ##############################################
465 # _array_has_eq( $value, \@array ) #
466 # ========================================== #
467 # $value - the value to check for #
468 # $array - the array to search in #
469 # ------------------------------------------ #
470 # returns true if the value exists in the #
471 # array provided. #
472 ##############################################
473  
474 sub _array_has_eq {
475 0 ▶ my ($value, $array) = @_;
Subroutine _array_has_eq: not called CC=3 SCAR=24.8
476  
477 0 foreach (@$array) {
478 0 ▶ return 1 if $_ eq $value;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $value 0 0 0
479 }
480  
481 0 return;
482 }
483  
484 ##############################################
485 # _array_has_re( $regex, \@array ) #
486 # ========================================== #
487 # $regex - the regex to check for #
488 # $array - the array to search in #
489 # ------------------------------------------ #
490 # returns true if a value exists in the #
491 # array provided that matches the regex. #
492 ##############################################
493  
494 sub _array_has_re {
495 0 ▶ my ($re, $array) = @_;
Subroutine _array_has_re: not called CC=3 SCAR=24.8
496  
497 0 foreach (@$array) {
498 0 ▶ return 1 if m/$re/;
Branch: 1 branch
BranchTrueFalseTotal
if /$re/ 0 0 0
499 }
500  
501 0 return;
502 }
503  
504 ##############################################
505 # _has_adv_que( \%hash ) #
506 # ========================================== #
507 # $hash - the hash-ref to search in #
508 # ------------------------------------------ #
509 # returns true if the hash-ref has any of #
510 # the lang's advanced query operators #
511 ##############################################
512  
513 sub _has_adv_que {
514 92 ▶ my $hash = shift;
Subroutine _has_adv_que: called CC=3 SCAR=11.0
515  
516 92 foreach ('$gt', '$gte', '$lt', '$lte', '$all', '$exists', '$mod', '$eq', '$ne', '$in', '$nin', '$size', '$type') {
517 683 ▶ return 1 if exists $hash->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{$_} 90 593 683
518 }
519  
520 2 return;
521 }
522  
523 ##############################################
524 # _value_in( $value, \@array ) #
525 # ========================================== #
526 # $value - the value to check for #
527 # $array - the array to search in #
528 # ------------------------------------------ #
529 # returns true if the value is one of the #
530 # values from the array. #
531 ##############################################
532  
533 sub _value_in {
534 13 ▶ my ($value, $array) = @_;
Subroutine _value_in: called CC=10 SCAR=24.1
535  
536 13 foreach (@$array) {
537 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
538 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
539 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
540 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
541 }
542  
543 4 return;
544 }
545  
546 =head2 update_doc( \%document, \%update )
547  
548 Receives a document hash-ref and an update hash-ref, and updates the
549 document in-place according to the update hash-ref. Also returns the document
550 after the update. If the update hash-ref doesn't have any of the update
551 modifiers described by the language, then the update hash-ref is considered
552 as what the document should now be, and so will simply replace the document
553 hash-ref (once again, in accordance with MongoDB).
554  
555 See L<MQUL::Reference/"UPDATE STRUCTURE"> to learn about the structure of
556 update hash-refs.
557  
558 =cut
559  
560 sub update_doc {
561 20 ▶ my ($doc, $obj) = @_;
Subroutine update_doc: called CC=84 SCAR=51.5
Pod documented
562  
563 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
564 unless defined $doc && ref $doc && ref $doc eq 'HASH';
565 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
566 unless defined $obj && ref $obj && ref $obj eq 'HASH';
567  
568 # we only need to do something if the $obj hash-ref has any advanced
569 # update operations, otherwise $obj is meant to be the new $doc
570  
571 15 ▶ if (&_has_adv_upd($obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_has_adv_upd($obj)) { } 14 1 15
572 14 foreach my $op (keys %$obj) {
573 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
574 # increase numerically
575 2 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 2 2
576 2 foreach my $field (keys %{$obj->{$op}}) {
577 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
578 2 $doc->{$field} += $obj->{$op}->{$field};
579 }
580 } elsif ($op eq '$set') {
581 # set key-value pairs
582 2 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 2 2
583 2 foreach my $field (keys %{$obj->{$op}}) {
584 3 $doc->{$field} = $obj->{$op}->{$field};
585 }
586 } elsif ($op eq '$unset') {
587 # remove key-value pairs
588 2 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 2 2
589 2 foreach my $field (keys %{$obj->{$op}}) {
590 2 ▶ delete $doc->{$field} if $obj->{$op}->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{$op}{$field} 2 0 2
591 }
592 } elsif ($op eq '$rename') {
593 # rename attributes
594 1 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 1 1
595 1 foreach my $field (keys %{$obj->{$op}}) {
596 $doc->{$obj->{$op}->{$field}} = delete $doc->{$field}
597 1 ▶ if exists $doc->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if exists $doc->{$field} 1 0 1
598 }
599 } elsif ($op eq '$push') {
600 # push values to end of arrays
601 1 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 1 1
602 1 foreach my $field (keys %{$obj->{$op}}) {
603 croak "The $field attribute is not an array in the doc."
604 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
605 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
606 1 push(@{$doc->{$field}}, $obj->{$op}->{$field});
607 }
608 } elsif ($op eq '$pushAll') {
609 # push a list of values to end of arrays
610 1 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 1 1
611 1 foreach my $field (keys %{$obj->{$op}}) {
612 croak "The $field attribute is not an array in the doc."
613 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
614 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
615 1 push(@{$doc->{$field}}, @{$obj->{$op}->{$field}});
616 }
617 } elsif ($op eq '$addToSet') {
618 # push values to arrays only if they're not already there
619 2 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 2 2
620 2 foreach my $field (keys %{$obj->{$op}}) {
621 croak "The $field attribute is not an array in the doc."
622 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
623 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
624 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
625 2 foreach my $val (@add) {
626 2 push(@{$doc->{$field}}, $val)
627 4 ▶ unless defined &_index_of($val, $doc->{$field});
Branch: 1 branch
BranchTrueFalseTotal
unless defined &_index_of($val, $doc->{$field}) 2 2 4
628 }
629 }
630 } elsif ($op eq '$pop') {
631 # pop the last item from an array
632 2 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 2 2
633 2 foreach my $field (keys %{$obj->{$op}}) {
634 croak "The $field attribute is not an array in the doc."
635 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
636 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
637 2 pop(@{$doc->{$field}})
638 2 ▶ if $obj->{$op}->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{$op}{$field} 2 0 2
639 }
640 } elsif ($op eq '$shift') {
641 # shift the first item from an array
642 1 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 1 1
643 1 foreach my $field (keys %{$obj->{$op}}) {
644 croak "The $field attribute is not an array in the doc."
645 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
646 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
647 1 shift(@{$doc->{$field}})
648 1 ▶ if $obj->{$op}->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{$op}{$field} 1 0 1
649 }
650 } elsif ($op eq '$splice') {
651 # splice offsets from arrays
652 1 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 1 1
653 1 foreach my $field (keys %{$obj->{$op}}) {
654 croak "The $field attribute is not an array in the doc."
655 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
656 next unless ref $obj->{$op}->{$field} &&
657 ref $obj->{$op}->{$field} eq 'ARRAY' &&
658 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
659 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
660 1 splice(@{$doc->{$field}}, $obj->{$op}->{$field}->[0], $obj->{$op}->{$field}->[1]);
661 }
662 } elsif ($op eq '$pull') {
663 # remove values from arrays
664 2 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 2 2
665 2 foreach my $field (keys %{$obj->{$op}}) {
666 croak "The $field attribute is not an array in the doc."
667 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
668 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
669 2 my $i = &_index_of($obj->{$op}->{$field}, $doc->{$field});
670 2 ▶ while (defined $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i) 2 2 4
671 2 splice(@{$doc->{$field}}, $i, 1);
672 2 $i = &_index_of($obj->{$op}->{$field}, $doc->{$field});
673 }
674 }
675 } elsif ($op eq '$pullAll') {
676 # remove a list of values from arrays
677 1 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 1 1
678 1 foreach my $field (keys %{$obj->{$op}}) {
679 croak "The $field attribute is not an array in the doc."
680 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
681 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
682 1 foreach my $value (@{$obj->{$op}->{$field}}) {
683 2 my $i = &_index_of($value, $doc->{$field});
684 2 ▶ while (defined $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i) 2 2 4
685 2 splice(@{$doc->{$field}}, $i, 1);
686 2 $i = &_index_of($value, $doc->{$field});
687 }
688 }
689 }
690 }
691 }
692 } else {
693 # $obj is actually the new $doc
694 1 %$doc = %$obj;
695 }
696  
697 15 return $doc;
698 }
699  
700 ##############################################
701 # _has_adv_upd( \%hash ) #
702 # ========================================== #
703 # $hash - the hash-ref to search in #
704 # ------------------------------------------ #
705 # returns true if the hash-ref has any of #
706 # the lang's advanced update operators #
707 ##############################################
708  
709 sub _has_adv_upd {
710 15 ▶ my $hash = shift;
Subroutine _has_adv_upd: called CC=3 SCAR=11.0
711  
712 15 foreach ('$inc', '$set', '$unset', '$push', '$pushAll', '$addToSet', '$pop', '$shift', '$splice', '$pull', '$pullAll', '$rename', '$bit') {
713 98 ▶ return 1 if exists $hash->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{$_} 14 84 98
714 }
715  
716 1 return;
717 }
718  
719 ##############################################
720 # _index_of( $value, \@array ) #
721 # ========================================== #
722 # $value - the value to search for #
723 # $array - the array to search in #
724 # ------------------------------------------ #
725 # searches for the provided value in the #
726 # array, and returns its index if it is #
727 # found, or undef otherwise. #
728 ##############################################
729  
730 sub _index_of {
731 12 ▶ my ($value, $array) = @_;
Subroutine _index_of: called CC=6 SCAR=20.3
732  
733 12 ▶ for (my $i = 0; $i < scalar @$array; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$array) 22 6 28
734 22 ▶ if (is_float($array->[$i]) && is_float($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_float($array->[$i]) and &is_float($value)) { } 0 22 22
Condition33%1/3
&is_float($array->[$i]) and &is_float($value)
!ll&&!rl&&r
2200
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
735 0 ▶ return $i if $array->[$i] == $value;
Branch: 1 branch
BranchTrueFalseTotal
if $array->[$i] == $value 0 0 0
736 } else {
737 22 ▶ return $i if $array->[$i] eq $value;
Branch: 1 branch
BranchTrueFalseTotal
if $array->[$i] eq $value 6 16 22
738 }
739 }
740  
741 6 return;
742 }
743  
744 ##############################################
745 # _parse_function( $doc, $key ) #
746 # ========================================== #
747 # $doc - the document #
748 # $key - the key referencing a function and #
749 # a list of attributes, such as #
750 # min(attr1, attr2, attr3) #
751 # ------------------------------------------ #
752 # calculates the value using the appropriate #
753 # function and returns the result #
754 ##############################################
755  
756 sub _parse_function {
757 23 ▶ my ($doc, $def) = @_;
Subroutine _parse_function: called CC=6 SCAR=17.9
758  
759 23 my ($func) = keys %$def;
760  
761 die "Unrecognized function $func"
762 23 ▶ unless exists $BUILTINS{$func};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $MQUL::BUILTINS{$func} 0 23 23
763  
764 $def->{$func} = [$def->{$func}]
765 23 ▶ unless ref $def->{$func};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $def->{$func} 4 19 23
766  
767 23 my @vals;
768 23 foreach (@{$def->{$func}}) {
769 53 my ($v, $k) = _expand_dot_notation($doc, $_);
770 53 ▶ push(@vals, $v)
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 49 4 53
771 if defined $v;
772 }
773  
774 23 ▶ return unless scalar @vals;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @vals 2 21 23
775  
776 21 return $BUILTINS{$func}->(@vals);
777 }
778  
779 ##############################################
780 # _expand_dot_notation( $doc, $key ) #
781 # ========================================== #
782 # $doc - the document #
783 # $key - the key using dot notation #
784 # ------------------------------------------ #
785 # takes a key using the dot notation, and #
786 # returns the value of the document at the #
787 # end of the chain (if any), plus the key at #
788 # the end of the chain. #
789 ##############################################
790  
791 sub _expand_dot_notation {
792 70 ▶ my ($doc, $key) = @_;
Subroutine _expand_dot_notation: called CC=10 SCAR=23.0
793  
794 70 ▶ return ($doc->{$key}, $key)
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /\./ 42 28 70
795 unless $key =~ m/\./;
796  
797 28 my @way_there = split(/\./, $key);
798  
799 28 $key = shift @way_there;
800 28 my %virt = ( $key => $doc->{$key} );
801  
802 28 ▶ while (scalar @way_there) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @way_there) 50 28 78
803 50 $key = shift @way_there;
804 50 my ($have) = values %virt;
805  
806 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
807 35 %virt = ( $key => $have->{$key} );
808 ▶ } 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
809 10 %virt = ( $key => $have->[$key] )
810 } else {
811 5 %virt = ();
812 }
813 }
814  
815 28 return ($virt{$key}, $key);
816 }
817  
818 =head1 DIAGNOSTICS
819  
820 =over
821  
822 =item C<< MQUL::doc_matches() requires a document hash-ref. >>
823  
824 This error means that you've either haven't passed the C<doc_matches()>
825 subroutine any parameters, or given it a non-hash-ref document.
826  
827 =item C<< MQUL::doc_matches() expects a query hash-ref. >>
828  
829 This error means that you've passed the C<doc_matches()> attribute a
830 non-hash-ref query variable. While you don't actually have to pass a
831 query variable, if you do, it has to be a hash-ref.
832  
833 =item C<< MQUL::update_doc() requires a document hash-ref. >>
834  
835 This error means that you've either haven't passed the C<update_doc()>
836 subroutine any parameters, or given it a non-hash-ref document.
837  
838 =item C<< MQUL::update_doc() requires an update hash-ref. >>
839  
840 This error means that you've passed the C<update_doc()> subroutine a
841 non-hash-ref update variable.
842  
843 =item C<< The %s attribute is not an array in the doc. >>
844  
845 This error means that your update hash-ref tries to modify an array attribute
846 (with C<$push>, C<$pushAll>, C<$addToSet>, C<$pull>, C<$pullAll>,
847 C<$pop>, C<$shift> and C<$splice>), but the attribute in the document
848 provided to the C<update_doc()> subroutine is not an array.
849  
850 =back
851  
852 =head1 CONFIGURATION AND ENVIRONMENT
853
854 MQUL requires no configuration files or environment variables.
855  
856 =head1 DEPENDENCIES
857  
858 MQUL depends on the following modules:
859  
860 =over
861  
862 =item * L<Data::Compare>
863  
864 =item * L<Data::Types>
865  
866 =item * L<DateTime::Format::W3CDTF>
867  
868 =item * L<Scalar::Util>
869  
870 =item * L<Try::Tiny>
871  
872 =back
873  
874 =head1 INCOMPATIBILITIES
875  
876 None reported.
877  
878 =head1 BUGS AND LIMITATIONS
879  
880 No bugs have been reported.
881  
882 Please report any bugs or feature requests to
883 C<bug-MQUL@rt.cpan.org>, or through the web interface at
884 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MQUL>.
885  
886 =head1 AUTHOR
887  
888 Ido Perlmuter <ido at ido50 dot net>
889  
890 =head1 LICENSE AND COPYRIGHT
891  
892 Copyright (c) 2011-2015, Ido Perlmuter C<< ido at ido50 dot net >>.
893  
894 This module is free software; you can redistribute it and/or
895 modify it under the same terms as Perl itself, either version
896 5.8.1 or any later version. See L<perlartistic|perlartistic>
897 and L<perlgpl|perlgpl>.
898  
899 The full text of the license can be found in the
900 LICENSE file included with this module.
901  
902 =head1 DISCLAIMER OF WARRANTY
903  
904 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
905 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
906 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
907 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
908 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
909 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
910 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
911 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
912 NECESSARY SERVICING, REPAIR, OR CORRECTION.
913  
914 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
915 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
916 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
917 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
918 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
919 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
920 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
921 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
922 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
923 SUCH DAMAGES.
924  
925 =cut
926  
927 3 1;
928 __END__
↑ Summary