| 1 | package JSON::Path; | ||||||||||||||||||||||||||||
| 2 | 14 | $JSON::Path::VERSION = '1.0.6'; | |||||||||||||||||||||||||||
| 3 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 14 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # VERSION | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 14 | ▶ | use Exporter::Shiny qw/ jpath jpath1 jpath_map /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 14 | our $AUTHORITY = 'cpan:POPEFELIX'; | |||||||||||||||||||||||||||
| 10 | 14 | our $Safe = 1; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 14 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 14 | ▶ | use JSON::MaybeXS qw/decode_json/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 14 | ▶ | use JSON::Path::Evaluator; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 14 | ▶ | use Scalar::Util qw[blessed]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 14 | ▶ | use LV (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 14 | ▶ | use overload '""' => \&to_string; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 3 | ▶ | sub jpath { | ||||||||||||||||||||||||||
|
Subroutine
jpath: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 3 | my ( $object, $expression ) = @_; | |||||||||||||||||||||||||||
| 22 | 3 | my @return = __PACKAGE__->new($expression)->values($object); | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 4 | ▶ | sub jpath1 : lvalue { | ||||||||||||||||||||||||||
|
Subroutine
jpath1: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 4 | my ( $object, $expression ) = @_; | |||||||||||||||||||||||||||
| 27 | 4 | __PACKAGE__->new($expression)->value($object); | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | sub jpath_map (&$$) { | ||||||||||||||||||||||||||
|
Subroutine
jpath_map: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 1 | my ( $coderef, $object, $expression ) = @_; | |||||||||||||||||||||||||||
| 32 | 1 | return __PACKAGE__->new($expression)->map( $object, $coderef ); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 34 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 34 | my ( $class, $expression ) = @_; | |||||||||||||||||||||||||||
| 37 | 34 | ▶ | return $expression | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($expression) and $expression->isa(__PACKAGE__)
MC/DC0%0/2
builtin::blessed($expression) and $expression->isa(__PACKAGE__)
builtin::blessed($expression)
$expression->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= builtin::blessed($expression)B= $expression->isa(__PACKAGE__)
| |||||||||||||||||||||||||||||
| 38 | if blessed($expression) && $expression->isa(__PACKAGE__); | ||||||||||||||||||||||||||||
| 39 | 34 | return bless \$expression, $class; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 74 | ▶ | sub to_string { | ||||||||||||||||||||||||||
|
Subroutine
to_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 74 | my ($self) = @_; | |||||||||||||||||||||||||||
| 44 | 74 | return $$self; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 7 | ▶ | sub paths { | ||||||||||||||||||||||||||
|
Subroutine
paths: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 7 | my ( $self, $object ) = @_; | |||||||||||||||||||||||||||
| 49 | 7 | my @paths = JSON::Path::Evaluator::evaluate_jsonpath( $object, "$self", want_path => 1); | |||||||||||||||||||||||||||
| 50 | 7 | return @paths; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 9 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 9 | my ( $self, $object ) = @_; | |||||||||||||||||||||||||||
| 55 | 9 | my @values = $self->values($object); | |||||||||||||||||||||||||||
| 56 | 9 | ▶ | return wantarray ? @values : $values[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 10 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=6 SCAR=19.1
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 10 | my ( $self, $object, $value, $limit ) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 10 | ▶ | if ( !ref $object ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | # warn if not called internally. If called internally (i.e. from value()) we will already have warned. | ||||||||||||||||||||||||||||
| 64 | 0 | my @c = caller(0); | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ( $c[1] !~ /JSON\/Path\.pm$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | carp qq{Useless attempt to set a value on a non-reference}; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 10 | my $count = 0; | |||||||||||||||||||||||||||
| 70 | 10 | my @refs = JSON::Path::Evaluator::evaluate_jsonpath( $object, "$self", want_ref => 1 ); | |||||||||||||||||||||||||||
| 71 | 10 | for my $ref (@refs) { | |||||||||||||||||||||||||||
| 72 | 12 | ${$ref} = $value; | |||||||||||||||||||||||||||
| 73 | 12 | ++$count; | |||||||||||||||||||||||||||
| 74 | 12 | ▶ | last if $limit && ( $count >= $limit ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$limit and $count >= $limit
MC/DC100%2/2
$limit and $count >= $limit
$limit
$count >= $limit
Truth table100%3/3
A and B
A= $limitB= $count >= $limit
| |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 10 | return $count; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 13 | ▶ | sub value : lvalue { | ||||||||||||||||||||||||||
|
Subroutine
value: called CC=1 SCAR=0.4
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 13 | my ( $self, $object ) = @_; | |||||||||||||||||||||||||||
| 81 | LV::lvalue( | ||||||||||||||||||||||||||||
| 82 | 8 | ▶ | get => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | 8 | my ($value) = $self->get($object); | |||||||||||||||||||||||||||
| 84 | 8 | return $value; | |||||||||||||||||||||||||||
| 85 | }, | ||||||||||||||||||||||||||||
| 86 | 6 | ▶ | set => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 87 | 6 | my $value = shift; | |||||||||||||||||||||||||||
| 88 | # do some caller() magic to warn at the right place | ||||||||||||||||||||||||||||
| 89 | 6 | ▶ | if ( !ref $object ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | my @c = caller(2); | |||||||||||||||||||||||||||
| 91 | 0 | my ( $filename, $line ) = @c[ 1, 2 ]; | |||||||||||||||||||||||||||
| 92 | 0 | warn qq{Useless attempt to set a value on a non-reference at $filename line $line\n}; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 6 | $self->set( $object, $value, 1 ); | |||||||||||||||||||||||||||
| 95 | }, | ||||||||||||||||||||||||||||
| 96 | 13 | ); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 28 | ▶ | sub values { | ||||||||||||||||||||||||||
|
Subroutine
values: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 28 | my ( $self, $object ) = @_; | |||||||||||||||||||||||||||
| 101 | 28 | ▶ | croak q{non-safe evaluation, died} if "$self" =~ /\?\(/ && $JSON::Path::Safe; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
"$self" =~ /\?\(/ and $JSON::Path::Safe
MC/DC100%2/2
"$self" =~ /\?\(/ and $JSON::Path::Safe
"$self" =~ /\?\(/
$JSON::Path::Safe
Truth table100%3/3
A and B
A= "$self" =~ /\?\(/B= $JSON::Path::Safe
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 27 | return JSON::Path::Evaluator::evaluate_jsonpath( $object, "$self", script_engine => 'perl' ); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 1 | ▶ | sub map { | ||||||||||||||||||||||||||
|
Subroutine
map: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 1 | my ( $self, $object, $coderef ) = @_; | |||||||||||||||||||||||||||
| 108 | 1 | my $count; | |||||||||||||||||||||||||||
| 109 | 1 | foreach my $path ( $self->paths( $object ) ) { | |||||||||||||||||||||||||||
| 110 | 4 | my ($ref) = JSON::Path::Evaluator::evaluate_jsonpath( $object, $path, want_ref => 1 ); | |||||||||||||||||||||||||||
| 111 | 4 | ++$count; | |||||||||||||||||||||||||||
| 112 | 4 | my $value = do { | |||||||||||||||||||||||||||
| 113 | 14 | ▶ | no warnings 'numeric'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 114 | 4 | local $_ = ${$ref}; | |||||||||||||||||||||||||||
| 115 | 4 | local $. = $path; | |||||||||||||||||||||||||||
| 116 | 4 | scalar $coderef->(); | |||||||||||||||||||||||||||
| 117 | }; | ||||||||||||||||||||||||||||
| 118 | 4 | ${$ref} = $value; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 1 | return $count; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 14 | 1; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | __END__ | ||||||||||||||||||||||||||||