| 1 | package JSON::Path::Evaluator; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 19 | $JSON::Path::Evaluator::VERSION = '1.0.2'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 19 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 19 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # ABSTRACT: A module that recursively evaluates JSONPath expressions with native support for Javascript-style filters | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 19 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 19 | ▶ | use Carp::Assert qw(assert); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 19 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 19 | ▶ | use JSON::Path::Constants qw(:operators :symbols); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 19 | ▶ | use JSON::Path::Tokenizer qw(tokenize); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 19 | ▶ | use List::Util qw/pairs uniq/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 19 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 19 | ▶ | use Safe; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 19 | ▶ | use Scalar::Util qw/looks_like_number blessed refaddr/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 19 | ▶ | use Storable qw/dclone/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 19 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 19 | ▶ | use Exporter::Shiny qw/evaluate_jsonpath/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 19 | our $AUTHORITY = 'cpan:POPEFELIX'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 19 | Readonly my $OPERATOR_IS_TRUE => 'IS_TRUE'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 19 | Readonly my $OPERATOR_TYPE_PATH => 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 19 | Readonly my $OPERATOR_TYPE_COMPARISON => 2; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 19 | Readonly my %OPERATORS => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | $TOKEN_ROOT => $OPERATOR_TYPE_PATH, # $ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | $TOKEN_CURRENT => $OPERATOR_TYPE_PATH, # @ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | $TOKEN_CHILD => $OPERATOR_TYPE_PATH, # . OR [] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | $TOKEN_RECURSIVE => $OPERATOR_TYPE_PATH, # .. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | $TOKEN_ALL => $OPERATOR_TYPE_PATH, # * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | $TOKEN_FILTER_OPEN => $OPERATOR_TYPE_PATH, # ?( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | $TOKEN_SCRIPT_OPEN => $OPERATOR_TYPE_PATH, # ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | $TOKEN_FILTER_SCRIPT_CLOSE => $OPERATOR_TYPE_PATH, # ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | $TOKEN_SUBSCRIPT_OPEN => $OPERATOR_TYPE_PATH, # [ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | $TOKEN_SUBSCRIPT_CLOSE => $OPERATOR_TYPE_PATH, # ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | $TOKEN_UNION => $OPERATOR_TYPE_PATH, # , | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | $TOKEN_ARRAY_SLICE => $OPERATOR_TYPE_PATH, # [ start:end:step ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | $TOKEN_SINGLE_EQUAL => $OPERATOR_TYPE_COMPARISON, # = | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | $TOKEN_DOUBLE_EQUAL => $OPERATOR_TYPE_COMPARISON, # == | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | $TOKEN_TRIPLE_EQUAL => $OPERATOR_TYPE_COMPARISON, # === | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | $TOKEN_GREATER_THAN => $OPERATOR_TYPE_COMPARISON, # > | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | $TOKEN_LESS_THAN => $OPERATOR_TYPE_COMPARISON, # < | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | $TOKEN_NOT_EQUAL => $OPERATOR_TYPE_COMPARISON, # != | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | $TOKEN_GREATER_EQUAL => $OPERATOR_TYPE_COMPARISON, # >= | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | $TOKEN_LESS_EQUAL => $OPERATOR_TYPE_COMPARISON, # <= | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 19 | Readonly my $ASSERT_ENABLE => $ENV{ASSERT_ENABLE}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 128 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=22.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 128 | ▶ | my %args = ref $_[0] eq 'HASH' ? %{ $_[0] } : @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 128 | my $self = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 128 | for my $key (qw/root expression/) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 256 | ▶ | croak qq{Missing required argument '$key' in constructor} unless $args{$key}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 256 | $self->{$key} = $args{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 128 | ▶ | $self->{want_ref} = $args{want_ref} || 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'want_ref'} || 0
MC/DC0%0/1
$args{'want_ref'} || 0
$args{'want_ref'}
Truth table50%1/2
A
A= $args{'want_ref'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 128 | ▶ | $self->{_calling_context} = $args{_calling_context} || 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'_calling_context'} || 0
MC/DC0%0/1
$args{'_calling_context'} || 0
$args{'_calling_context'}
Truth table50%1/2
A
A= $args{'_calling_context'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | my $script_engine = | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 128 | ▶ | $args{script_engine} ? $args{script_engine} : $self->{expression} =~ /\$_/ ? 'perl' : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 128 | ▶ | $self->{script_engine} = $script_engine || 'PseudoJS'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$script_engine || 'PseudoJS'
MC/DC100%1/1
$script_engine || 'PseudoJS'
$script_engine
Truth table100%2/2
A
A= $script_engine
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 128 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 128 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | sub evaluate_jsonpath { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 128 | ▶ | my ( $json_object, $expression, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
evaluate_jsonpath: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 128 | ▶ | if ( !ref $json_object ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | try { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 71 | ▶ | $json_object = decode_json($json_object); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | croak qq{Unable to decode $json_object as JSON: $_}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 71 | } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 128 | ▶ | my $want_ref = delete $args{want_ref} || 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $args{'want_ref'} || 0
MC/DC100%1/1
delete $args{'want_ref'} || 0
delete $args{'want_ref'}
Truth table100%2/2
A
A= delete $args{'want_ref'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 128 | ▶ | my $want_path = delete $args{want_path} || 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $args{'want_path'} || 0
MC/DC100%1/1
delete $args{'want_path'} || 0
delete $args{'want_path'}
Truth table100%2/2
A
A= delete $args{'want_path'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 128 | ▶ | my $self = __PACKAGE__->new( | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | root => $json_object, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | expression => $expression, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | _calling_context => wantarray ? 'ARRAY' : 'SCALAR', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | %args | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 128 | return $self->evaluate( $expression, want_ref => $want_ref, want_path => $want_path ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | sub evaluate { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 128 | ▶ | my ( $self, $expression, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
evaluate: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 128 | my $json_object = $self->{root}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 128 | my $token_stream = [ tokenize($expression) ]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | #use Data::Dumper qw/Dumper/; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | #print Dumper $token_stream; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 128 | ▶ | shift @{$token_stream} if $token_stream->[0] eq $TOKEN_ROOT; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 128 | ▶ | shift @{$token_stream} if $token_stream->[0] eq $TOKEN_CHILD; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 128 | ▶ | if ( $args{want_path} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 11 | my %reftable = $self->_reftable_walker($json_object); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 11 | my @refs = $self->_evaluate( $json_object, dclone $token_stream, 1 ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 11 | my @paths; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 11 | for my $ref (@refs) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 17 | ▶ | my $refaddr = ref ${$ref} ? refaddr ${$ref} : refaddr $ref; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 17 | push @paths, $reftable{$refaddr}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 11 | return @paths; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 117 | my @ret = $self->_evaluate( $json_object, $token_stream, $args{want_ref} ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 117 | return @ret; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | sub _reftable_walker { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 77 | ▶ | my ( $self, $json_object, $base_path ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reftable_walker: called CC=9 SCAR=22.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 77 | ▶ | $base_path //= '$'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$base_path //= "\$"
MC/DC100%1/1
$base_path //= "\$"
$base_path
Truth table100%2/2
A
A= $base_path
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 77 | ▶ | $json_object //= $self->root; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$json_object //= $self->root
MC/DC0%0/2
$json_object //= $self->root
$json_object
$self->root
Truth table33%1/3
A //= B
A= $json_objectB= $self->root
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 77 | my @entries = ( refaddr $json_object => $base_path ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 77 | ▶ | if ( _arraylike($json_object) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 8 | for ( 0 .. $#{$json_object} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 30 | my $path = sprintf q{%s['%d']}, $base_path, $_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 30 | ▶ | if ( ref $json_object->[$_] ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 30 | push @entries, $self->_reftable_walker( $json_object->[$_], $path ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | push @entries, refaddr \( $json_object->[$_] ) => $path; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | elsif ( _hashlike($json_object) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 63 | for my $index ( keys %{$json_object} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 234 | my $path = sprintf q{%s['%s']}, $base_path, $index; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 234 | ▶ | if ( ref $json_object->{$index} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 36 | push @entries, $self->_reftable_walker( $json_object->{$index}, $path ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 198 | push @entries, refaddr \( $json_object->{$index} ) => $path; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 77 | return @entries; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | sub _evaluate { # This assumes that the token stream is syntactically valid | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 767 | ▶ | my ( $self, $obj, $token_stream, $want_ref ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_evaluate: called CC=20 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 767 | ▶ | return unless ref $obj; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 683 | ▶ | $token_stream //= []; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$token_stream //= []
MC/DC0%0/1
$token_stream //= []
$token_stream
Truth table50%1/2
A
A= $token_stream
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 683 | ▶ | while ( defined( my $token = shift @{$token_stream} ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 1312 | ▶ | next if $token eq $TOKEN_CURRENT; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 1091 | ▶ | next if $token eq $TOKEN_CHILD; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 683 | ▶ | if ( $token eq $TOKEN_FILTER_OPEN ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 54 | my $filter_expression = shift @{$token_stream}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 54 | my $closing_token = shift @{$token_stream}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 54 | ▶ | assert( $closing_token eq $TOKEN_FILTER_SCRIPT_CLOSE, q{Closing token seen} ) if $ASSERT_ENABLE; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | # Find all indices matching the filter expression. This modifies $token_stream | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 54 | my @matching_indices = $self->_process_filter( $obj, $filter_expression ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 54 | ▶ | if ( !@{$token_stream} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 20 | my @got = map { _get( $obj, $_ ) } @matching_indices; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 20 | ▶ | return $want_ref ? @got : map { ${$_} } @got; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 34 | return map { $self->_evaluate( _get( $obj, $_ ), dclone($token_stream), $want_ref ) } @matching_indices; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | elsif ( $token eq $TOKEN_RECURSIVE ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | { # Sweet Jesus, Pooh, that's not honey! You're eating Sweet Jesus, Pooh, that's not honey! You're eating... | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 16 | my $index = shift @{$token_stream}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 16 | my @matched; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 16 | ▶ | if ( $index eq $TOKEN_FILTER_OPEN ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 2 | my $filter_expression = shift @{$token_stream}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 2 | my $closing_token = shift @{$token_stream}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 2 | ▶ | assert( $closing_token eq $TOKEN_FILTER_SCRIPT_CLOSE, q{Closing token seen} ) if $ASSERT_ENABLE; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 2 | return $self->_filter_recursive( $obj, $filter_expression, $want_ref ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 14 | @matched = _match_recursive( $obj, $index, $want_ref ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 14 | ▶ | if ( !@{$token_stream} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 6 | return @matched; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 8 | return map { $self->_evaluate( $_, dclone($token_stream), $want_ref ) } @matched; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 613 | my $index; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 613 | ▶ | if ( $token eq $TOKEN_SUBSCRIPT_OPEN ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 98 | $index = shift @{$token_stream}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 98 | my $closing_token = shift @{$token_stream}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 98 | ▶ | assert $closing_token eq $TOKEN_SUBSCRIPT_CLOSE if $ASSERT_ENABLE; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 515 | $index = $token; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 613 | ▶ | assert( !$OPERATORS{$index}, qq{"$index" is not an operator} ) if $index ne $TOKEN_ALL; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 613 | ▶ | assert( !ref $index, q{Index is a scalar} ) if $ASSERT_ENABLE; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 613 | my (@got) = _get( $obj, $index, create_key => $want_ref ); # This always returns a ref | |||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 613 | ▶ | if ( !@{$token_stream} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 356 | ▶ | return $want_ref ? @got : map { ${$_} } @got; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 257 | return map { $self->_evaluate( ${$_}, dclone($token_stream), $want_ref ) } @got; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | sub _process_filter { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 94 | ▶ | my ( $self, $obj, $filter_expression ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_filter: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 94 | my @matching_indices; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 94 | ▶ | if ( $self->{script_engine} eq 'PseudoJS' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 87 | @matching_indices = $self->_process_pseudo_js( $obj, $filter_expression ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | elsif ( $self->{script_engine} eq 'perl' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 7 | @matching_indices = $self->_process_perl( $obj, $filter_expression ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | croak qq{Unsupported script engine "$self->{script_engine}"}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 94 | return @matching_indices; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | # This _always_ has to return a ref so that when it's called from evaluate( ... , want_ref => 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | # So that we can return a ref into the object (e.g. for use as an lvalue), even when the path points | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | # to a scalar (which will of course be copied). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | # I.E.: for { foo => 'bar' }, we always want \( foo->{bar} ) so that | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | # JSON::Path->new('$.foo')->value($obj) = 'baz' works like it oughtta. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | sub _get { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 792 | ▶ | my ( $object, $index, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get: called CC=16 SCAR=27.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 792 | ▶ | assert( _hashlike($object) || _arraylike($object), 'Object is a hashref or an arrayref' ) if $ASSERT_ENABLE; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
_hashlike($object) || _arraylike($object)
MC/DC0%0/2
_hashlike($object) || _arraylike($object)
_hashlike($object)
_arraylike($object)
Truth table0%0/3
A || B
A= _hashlike($object)B= _arraylike($object)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 792 | my $create_key = $args{create_key}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | # When want_ref is passed to _evaluate(), it will return a reference to whatever was matched. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | # If what was matched is itself a ref (e.g. an arrayref), _evaluate() will return a ref of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | # type 'REF'. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 792 | ▶ | if ( ref $object eq 'REF' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 32 | $object = ${$object}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 792 | ▶ | if ( $index eq $TOKEN_ALL ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 29 | ▶ | if ( _hashlike($object) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 3 | return map { \($_) } values %{$object}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 26 | return map { \($_) } @{$object}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 763 | my @indices; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 763 | ▶ | if ( $index =~ /$TOKEN_ARRAY_SLICE/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 9 | ▶ | my $length = _hashlike($object) ? scalar values %{$object} : scalar @{$object}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 9 | @indices = _slice( $index, $length ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | elsif ( $index =~ /$TOKEN_UNION/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 5 | @indices = split /$TOKEN_UNION/, $index; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 749 | @indices = ($index); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 763 | ▶ | if ( _hashlike($object) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 607 | ▶ | if ($create_key) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 84 | return map { \( $object->{$_} ) } @indices; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 523 | my @ret; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 523 | for my $index (@indices) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 524 | ▶ | push @ret, \( $object->{$index} ) if exists $object->{$index}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 523 | return @ret; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 156 | my @numeric_indices = grep { looks_like_number($_) } @indices; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 156 | ▶ | if ($create_key) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 18 | return map { \( $object->[$_] ) } @numeric_indices; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 138 | my @ret; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 138 | for my $index (@numeric_indices) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 129 | ▶ | push @ret, \( $object->[$index] ) if exists $object->[$index]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 138 | return @ret; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | sub _indices { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 40 | ▶ | my $object = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_indices: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 27 | ▶ | _hashlike($object) ? keys %{$object} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 40 | ▶ | : _arraylike($object) ? ( 0 .. $#{$object} ) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | sub _hashlike { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 1236 | ▶ | my $object = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_hashlike: called CC=4 SCAR=15.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 1236 | ▶ | return ( ref $object eq 'HASH' || ( blessed $object && $object->can('typeof') && $object->typeof eq 'HASH' ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition55%5/9
ref $object eq 'HASH' || builtin::blessed($object) && $object->can('typeof') && $object->typeof eq 'HASH'
builtin::blessed($object) and $object->can("typeof") and $object->typeof eq "HASH"
builtin::blessed($object) and $object->can("typeof")
MC/DC25%1/4
ref $object eq 'HASH' || builtin::blessed($object) && $object->can('typeof') && $object->typeof eq 'HASH'
ref $object eq 'HASH'
builtin::blessed($object)
$object->can("typeof")
$object->typeof eq "HASH"
Truth table60%3/5
A || B and C and D
A= ref $object eq 'HASH'B= builtin::blessed($object)C= $object->can("typeof")D= $object->typeof eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | sub _arraylike { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 436 | ▶ | my $object = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_arraylike: called CC=4 SCAR=15.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 436 | ▶ | return ( ref $object eq 'ARRAY' || ( blessed $object && $object->can('typeof') && $object->typeof eq 'ARRAY' ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition55%5/9
ref $object eq 'ARRAY' || builtin::blessed($object) && $object->can('typeof') && $object->typeof eq 'ARRAY'
builtin::blessed($object) and $object->can("typeof") and $object->typeof eq "ARRAY"
builtin::blessed($object) and $object->can("typeof")
MC/DC25%1/4
ref $object eq 'ARRAY' || builtin::blessed($object) && $object->can('typeof') && $object->typeof eq 'ARRAY'
ref $object eq 'ARRAY'
builtin::blessed($object)
$object->can("typeof")
$object->typeof eq "ARRAY"
Truth table60%3/5
A || B and C and D
A= ref $object eq 'ARRAY'B= builtin::blessed($object)C= $object->can("typeof")D= $object->typeof eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | sub _get_token { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | my $token_stream = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_token: not called CC=13 SCAR=49.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | my $token = shift @{$token_stream}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | ▶ | return unless defined $token; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | ▶ | if ( $token eq $TOKEN_SUBSCRIPT_OPEN ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | my @substream; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | my $close_seen; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | ▶ | while ( defined( my $token = shift @{$token_stream} ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | ▶ | if ( $token eq $TOKEN_SUBSCRIPT_CLOSE ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | $close_seen = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | push @substream, $token; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 0 | ▶ | assert($close_seen) if $ASSERT_ENABLE; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | ▶ | if ( grep { $_ eq $TOKEN_ARRAY_SLICE } @substream ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | # There are five valid cases: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | # n:m -> n:m:1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | # n:m:s -> n:m:s | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | # :m -> 0:m:1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | # ::s -> 0:-1:s | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | # n: -> n:-1:1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 0 | ▶ | if ( $substream[0] eq $TOKEN_ARRAY_SLICE ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | unshift @substream, undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 19 | ▶ | no warnings qw/uninitialized/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | ▶ | if ( $substream[2] eq $TOKEN_ARRAY_SLICE ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 0 | @substream = ( @substream[ ( 0, 1 ) ], undef, @substream[ ( 2 .. $#substream ) ] ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 19 | ▶ | use warnings qw/uninitialized/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 0 | my ( $start, $end, $step ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | ▶ | $start = $substream[0] // 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$substream[0] // 0
MC/DC0%0/1
$substream[0] // 0
$substream[0]
Truth table0%0/2
A
A= $substream[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | ▶ | $end = $substream[2] // -1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$substream[2] // -1
MC/DC0%0/1
$substream[2] // -1
$substream[2]
Truth table0%0/2
A
A= $substream[2]
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | ▶ | $step = $substream[4] // 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$substream[4] // 1
MC/DC0%0/1
$substream[4] // 1
$substream[4]
Truth table0%0/2
A
A= $substream[4]
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 0 | return { slice => [ $start, $end, $step ] }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | elsif ( grep { $_ eq $TOKEN_UNION } @substream ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | my @union = grep { $_ ne $TOKEN_UNION } @substream; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | return { union => \@union }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 0 | return $substream[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | return $token; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | # See http://wiki.ecmascript.org/doku.php?id=proposals:slice_syntax | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | # in particular, for the slice [n:m], m is *one greater* than the last index to slice. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | # This means that the slice [3:5] will return indices 3 and 4, but *not* 5. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | sub _slice { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 9 | ▶ | my ( $index, $length ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_slice: called CC=10 SCAR=25.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 9 | my ( $start, $end, $step ) = split /$TOKEN_ARRAY_SLICE/, $index, 3; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 9 | ▶ | if ( !defined($start) || $start eq '' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $start or $start eq ""
MC/DC0%0/2
not defined $start or $start eq ""
not defined $start
$start eq ""
Truth table33%1/3
not(A) or B
A= not defined $startB= $start eq ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | $start = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 9 | ▶ | if ( !defined($end) || $end eq '' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $end or $end eq ""
MC/DC50%1/2
not defined $end or $end eq ""
not defined $end
$end eq ""
Truth table66%2/3
not(A) or B
A= not defined $endB= $end eq ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 6 | $end = -1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 9 | ▶ | if ( !defined($step) || $step eq '' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $step or $step eq ""
MC/DC0%0/2
not defined $step or $step eq ""
not defined $step
$step eq ""
Truth table33%1/3
not(A) or B
A= not defined $stepB= $step eq ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 9 | $step = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 9 | ▶ | $start = ( $length - 1 ) if $start == -1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 9 | ▶ | $end = $length if $end == -1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 9 | my @indices; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 9 | ▶ | if ( $step < 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | @indices = grep { %_ % -$step == 0 } reverse( $start .. ( $end - 1 ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 9 | @indices = grep { $_ % $step == 0 } ( $start .. ( $end - 1 ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 9 | return @indices; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | sub _match_recursive { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 281 | ▶ | my ( $obj, $index, $want_ref ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_match_recursive: called CC=13 SCAR=26.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 281 | my @match; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | # Fix for RT #122529. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | # Consider the expression "$..foo..bar", evaluated with respect to the JSON "{"foo":{"bar":"baz"}}". | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | # The first term to be evaluated in the expression is "$..foo". If want_ref is passed to evaluate(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | # this will return a REF reference. In that case we must first dereference it to get the object that | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | # we will evaluate "..bar" with respect to. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 281 | ▶ | if ( ref $obj eq 'REF' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 2 | $obj = ${$obj}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 281 | ▶ | if ( _arraylike($obj) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 82 | ▶ | if ( looks_like_number($index) && exists $obj->[$index] ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
&looks_like_number($index) and exists $obj->[$index]
MC/DC0%0/2
&looks_like_number($index) and exists $obj->[$index]
&looks_like_number($index)
exists $obj->[$index]
Truth table33%1/3
A and B
A= &looks_like_number($index)B= exists $obj->[$index]
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 0 | ▶ | push @match, $want_ref ? \( $obj->[$index] ) : $obj->[$index]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 82 | for ( 0 .. $#{$obj} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 251 | ▶ | next unless ref $obj->[$_]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 115 | push @match, _match_recursive( $obj->[$_], $index, $want_ref ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | elsif ( _hashlike($obj) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 191 | ▶ | if ( exists $obj->{$index} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 16 | ▶ | push @match, $want_ref ? \( $obj->{$index} ) : $obj->{$index}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 191 | for my $val ( values %{$obj} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 591 | ▶ | next unless ref $val; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 152 | push @match, _match_recursive( $val, $index, $want_ref ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 281 | return @match; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | sub _filter_recursive { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | 40 | ▶ | my ( $self, $obj, $expression, $want_ref ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_filter_recursive: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 40 | my @ret; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | # Evaluate the filter expression for the current object | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 40 | my @matching_indices = $self->_process_filter( $obj, $expression ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 40 | for my $index (@matching_indices) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 4 | my ($got) = _get( $obj, $index ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 4 | ▶ | push @ret, $want_ref ? $got : ${$got}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | # Evaluate the filter expression for any subordinate objects | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 40 | for my $index ( _indices($obj) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 122 | my ($got) = _get( $obj, $index ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | 122 | $got = ${$got}; # _get will always return a reference. We want the value, so dereference it | |||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 122 | ▶ | next unless ref $got; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | 38 | push @ret, $self->_filter_recursive( $got, $expression, $want_ref ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | 40 | return @ret; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | sub _process_pseudo_js { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | 87 | ▶ | my ( $self, $object, $expressions ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_pseudo_js: called CC=12 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | 87 | my @expressions_or = split /\Q||\E/, $expressions; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 87 | my @matching_or; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | 87 | foreach my $expression (@expressions_or) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | 97 | my @expressions_and = split /\Q&&\E/, $expression; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 97 | my %matching_and; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 97 | foreach my $expression (@expressions_and) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | 120 | my ( $lhs, $operator, $rhs ) = _parse_psuedojs_expression($expression); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | 120 | $lhs =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | 120 | my (@token_stream) = tokenize($lhs); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | 120 | ▶ | if ( _hashlike($object) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | 55 | ▶ | while (my ($k, $v) = each(%$object)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | 158 | my @got = $self->_evaluate( $v, [@token_stream] ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | 158 | foreach my $got (@got) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | 56 | ▶ | $matching_and{$k}++ if _compare( $operator, $got, $rhs ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | elsif ( _arraylike($object) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | 63 | my $idx = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | 63 | for my $value ( @{$object} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | 147 | my ($got) = $self->_evaluate( $value, [@token_stream] ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | 147 | ▶ | $matching_and{$idx}++ if _compare( $operator, $got, $rhs ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | 147 | $idx++; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 97 | ▶ | while (my ($idx, $val) = each(%matching_and)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | 71 | ▶ | push @matching_or, $idx if ($val == @expressions_and); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | 87 | return sort(uniq(@matching_or)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | sub _parse_psuedojs_expression { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | 120 | ▶ | my $expression = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_psuedojs_expression: called CC=12 SCAR=25.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | 120 | my @parts; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | 120 | my ( $lhs, $operator, $rhs ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | # The operator could be '=', '!=', '==', '===', '<=', or '>=' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | 120 | ▶ | if ( $expression =~ /$EQUAL_SIGN/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | 39 | my $position = index( $expression, '=' ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | 39 | ▶ | if ( substr( $expression, $position + 1, 1 ) eq $EQUAL_SIGN ) { # could be '==' or '===' | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | 26 | ▶ | if ( substr( $expression, $position + 2, 1 ) eq $EQUAL_SIGN ) { # === | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | 0 | $operator = $TOKEN_TRIPLE_EQUAL; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | 26 | $operator = $TOKEN_DOUBLE_EQUAL; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | 13 | my $preceding_char = substr( $expression, $position - 1, 1 ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | 13 | ▶ | if ( $preceding_char eq $GREATER_THAN_SIGN ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | 11 | $operator = $TOKEN_GREATER_EQUAL; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | elsif ( $preceding_char eq $LESS_THAN_SIGN ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | 0 | $operator = $TOKEN_LESS_EQUAL; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | elsif ( $preceding_char eq $EXCLAMATION_MARK ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | 0 | $operator = $TOKEN_NOT_EQUAL; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | 2 | $operator = $TOKEN_SINGLE_EQUAL; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | 39 | ( $lhs, $rhs ) = split /$operator/, $expression, 2; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | 81 | for ( grep { $OPERATORS{$_} eq $OPERATOR_TYPE_COMPARISON } keys %OPERATORS ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | 382 | ▶ | next if /$EQUAL_SIGN/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | 110 | ▶ | if ( $expression =~ /$_/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | 73 | ( $lhs, $rhs ) = split /$_/, $expression, 2; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | 73 | $operator = $_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | 73 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | # FIXME: RHS is assumed to be a single value. This isn't necessarily a safe assumption. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | 120 | ▶ | if ($operator) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | 112 | ▶ | $rhs = _normalize( $rhs || '' ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$rhs || ''
MC/DC100%1/1
$rhs || ''
$rhs
Truth table100%2/2
A
A= $rhs
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | 112 | $lhs = _normalize($lhs); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | 8 | $operator = $OPERATOR_IS_TRUE; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | 8 | $lhs = $expression; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | 120 | return ( $lhs, $operator, $rhs ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | sub _normalize { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | 224 | ▶ | my $string = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | # NB: Stripping spaces *before* stripping quotes allows the caller to quote spaces in an index. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | # So an index of 'foo ' will be correctly normalized as 'foo', but '"foo "' will normalize to 'foo '. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | 224 | $string =~ s/\s+$//; # trim trailing spaces | |||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | 224 | $string =~ s/^\s+//; # trim leading spaces | |||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | 224 | $string =~ s/^['"](.+)['"]$/$1/; # Strip quotes from index | |||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | 224 | return $string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | sub _process_perl { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | 7 | ▶ | my ( $self, $object, $code ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_perl: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | 7 | my $cpt = Safe->new; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | 7 | $cpt->permit_only( ':base_core', qw/padsv padav padhv padany rv2gv/ ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | 7 | ${ $cpt->varglob('root') } = dclone( $self->{root} ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | 7 | my @matching; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | 7 | ▶ | if ( _hashlike($object) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | 2 | for my $index ( keys %{$object} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | 10 | local $_ = $object->{$index}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | 10 | my $ret = $cpt->reval($code); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | 10 | ▶ | croak qq{Error in filter: $@} if $@; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | 10 | ▶ | push @matching, $index if $ret; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | 5 | for my $index ( 0 .. $#{$object} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | 17 | local $_ = $object->[$index]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | 17 | my $ret = $cpt->reval($code); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | 17 | ▶ | croak qq{Error in filter: $@} if $@; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | 17 | ▶ | push @matching, $index if $ret; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | 7 | return @matching; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | sub _compare { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | 203 | ▶ | my ( $operator, $lhs, $rhs ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_compare: called CC=19 SCAR=32.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 19 | ▶ | no warnings qw/uninitialized/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | 203 | ▶ | if ( $operator eq $OPERATOR_IS_TRUE ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | 15 | ▶ | return $lhs ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | 188 | ▶ | my $use_numeric = looks_like_number($lhs) && looks_like_number($rhs); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
&looks_like_number($lhs) && &looks_like_number($rhs)
MC/DC100%2/2
&looks_like_number($lhs) && &looks_like_number($rhs)
&looks_like_number($lhs)
&looks_like_number($rhs)
Truth table100%3/3
A && B
A= &looks_like_number($lhs)B= &looks_like_number($rhs)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | 188 | ▶ | if ( $operator eq '=' || $operator eq '==' || $operator eq '===' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$operator eq "=" or $operator eq "==" or $operator eq "==="
$operator eq "=" or $operator eq "=="
MC/DC66%2/3
$operator eq "=" or $operator eq "==" or $operator eq "==="
$operator eq "="
$operator eq "=="
$operator eq "==="
Truth table75%3/4
A or B or C
A= $operator eq "="B= $operator eq "=="C= $operator eq "==="
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | 63 | ▶ | return $use_numeric ? ( $lhs == $rhs ) : $lhs eq $rhs; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | 125 | ▶ | if ( $operator eq '<' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | 26 | ▶ | return $use_numeric ? ( $lhs < $rhs ) : $lhs lt $rhs; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | 99 | ▶ | if ( $operator eq '>' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | 77 | ▶ | return $use_numeric ? ( $lhs > $rhs ) : $lhs gt $rhs; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | 22 | ▶ | if ( $operator eq '<=' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | 0 | ▶ | return $use_numeric ? ( $lhs <= $rhs ) : $lhs le $rhs; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | 22 | ▶ | if ( $operator eq '>=' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | 22 | ▶ | return $use_numeric ? ( $lhs >= $rhs ) : $lhs ge $rhs; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | 0 | ▶ | if ( $operator eq '!=' || $operator eq '!==' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$operator eq "!=" or $operator eq "!=="
MC/DC0%0/2
$operator eq "!=" or $operator eq "!=="
$operator eq "!="
$operator eq "!=="
Truth table0%0/3
A or B
A= $operator eq "!="B= $operator eq "!=="
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | 0 | ▶ | return $use_numeric ? ( $lhs != $rhs ) : $lhs ne $rhs; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | 19 | ▶ | use warnings qw/uninitialized/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | 19 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||