blib/lib/JQ/Lite/Value.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 117 CC 20 SCAR 46.0
CC
20
Cov
0%
compare
156.0
type_of
90.0
equal
2.0
CRAP 99.8
1 package JQ::Lite::Value;
2  
3 use strict;
4 use warnings;
5  
6 use JSON::PP ();
7 use Scalar::Util qw(looks_like_number);
8 use B ();
9  
10 # Older B releases do not expose the public SV flag constants. The flag
11 # values themselves are part of Perl's long-standing SV layout, so retain
12 # their historical values when running with one of those releases.
13 my $SVf_IOK = B->can('SVf_IOK') ? B::SVf_IOK() : 0x00000100;
14 my $SVf_NOK = B->can('SVf_NOK') ? B::SVf_NOK() : 0x00000200;
15 my $SVf_POK = B->can('SVf_POK') ? B::SVf_POK() : 0x00000400;
16  
17 # This module is deliberately internal. It provides the jq-style value
18 # semantics needed by the 3.0 evaluator without changing the compatibility
19 # behaviour of the 2.x filters that still live in JQ::Lite::Util.
20  
21 sub type_of {
22 my ($value) = @_;
23  
24 return 'null' unless defined $value;
25 return 'boolean' if JSON::PP::is_bool($value);
26 return 'array' if ref($value) eq 'ARRAY';
27 return 'object' if ref($value) eq 'HASH';
28 if (!ref($value)) {
29 my $flags = B::svref_2object(\$value)->FLAGS;
30 # Test the public numeric flags first. Older Perl releases may promote
31 # the cached PV to SVf_POK during interpolation even though IOK/NOK is
32 # still present and JSON::PP continues to encode the scalar as a
33 # number. Numeric identity therefore wins for dual-valued scalars.
34 return 'number' if $flags & ($SVf_IOK | $SVf_NOK);
35 return 'string' if $flags & $SVf_POK;
36 return 'number' if looks_like_number($value);
37 }
38 return 'string';
39 }
40  
41 sub compare {
42 my ($left, $right) = @_;
43  
44 my %rank = (
45 null => 0,
46 boolean => 1,
47 number => 2,
48 string => 3,
49 array => 4,
50 object => 5,
51 );
52  
53 my $left_type = type_of($left);
54 my $right_type = type_of($right);
55 return $rank{$left_type} <=> $rank{$right_type}
56 if $left_type ne $right_type;
57  
58 return 0 if $left_type eq 'null';
59 return (!!$left) <=> (!!$right) if $left_type eq 'boolean';
60 return $left <=> $right if $left_type eq 'number';
61 return $left cmp $right if $left_type eq 'string';
62  
63 if ($left_type eq 'array') {
64 my $limit = @{$left} < @{$right} ? @{$left} : @{$right};
65 for my $index ($limit ? (0 .. $limit - 1) : ()) {
66 my $ordering = compare($left->[$index], $right->[$index]);
67 return $ordering if $ordering;
68 }
69 return @{$left} <=> @{$right};
70 }
71  
72 my @left_keys = sort keys %{$left};
73 my @right_keys = sort keys %{$right};
74 my $key_ordering = compare(\@left_keys, \@right_keys);
75 return $key_ordering if $key_ordering;
76 for my $key (@left_keys) {
77 my $ordering = compare($left->{$key}, $right->{$key});
78 return $ordering if $ordering;
79 }
80 return 0;
81 }
82  
83 sub equal {
84 my ($left, $right) = @_;
85 return compare($left, $right) == 0;
86 }
87  
88 1;