| 1 | package Acrux::Pointer; | ||||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||
| 5 | =encoding utf8 | ||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | Acrux::Pointer - The Acrux pointer | ||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | use Acrux::Pointer; | ||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | my $pointer = Acrux::Pointer->new(data => {foo => [123, 'bar']}); | ||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | say $pointer->get('/foo/1'); | ||||||||||||||||||||||||||||||||||
| 18 | say 'Contains "/foo".' if $pointer->contains('/foo'); | ||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | This class is an implementation of L<RFC 6901|https://tools.ietf.org/html/rfc6901> | ||||||||||||||||||||||||||||||||||
| 23 | for perl hash-structures | ||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||
| 25 | =head2 new | ||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||
| 27 | my $pointer = Acrux::Pointer->new; | ||||||||||||||||||||||||||||||||||
| 28 | my $pointer = Acrux::Pointer->new(data => {foo => 'bar'}); | ||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | Build new Acrux::Pointer object | ||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||
| 32 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||
| 34 | This class implements the following attributes | ||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | =head2 data | ||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||
| 38 | my $data = $pointer->data; | ||||||||||||||||||||||||||||||||||
| 39 | $pointer = $pointer->data({foo => 'bar'}); | ||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||
| 41 | Data structure to be processed | ||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||
| 43 | =head1 METHODS | ||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||
| 45 | This class implements the following methods | ||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||
| 47 | =head2 contains | ||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | my $bool = $pointer->contains('/foo/1'); | ||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | Check if L</"data"> contains a value that can be identified with the given pointer | ||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | =head2 get | ||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||
| 55 | my $value = $pointer->get('/foo/bar'); | ||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||
| 57 | Extract value from L</"data"> identified by the given pointer | ||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | # "just a string" | ||||||||||||||||||||||||||||||||||
| 60 | Acrux::Pointer->new(data => 'just a string')->get(); | ||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | # "bar" | ||||||||||||||||||||||||||||||||||
| 63 | Acrux::Pointer->new(data => {foo => 'bar', baz => [4, 5, 6]})->get('/foo'); | ||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | # "4" | ||||||||||||||||||||||||||||||||||
| 66 | Acrux::Pointer->new(data => {foo => 'bar', baz => [4, 5, 6]})->get('/baz/0'); | ||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||
| 68 | # "6" | ||||||||||||||||||||||||||||||||||
| 69 | Acrux::Pointer->new(data => {foo => 'bar', baz => [4, 5, 6]})->get('/baz/2'); | ||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||
| 71 | =head1 HISTORY | ||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||
| 73 | See C<Changes> file | ||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||
| 75 | =head1 TO DO | ||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||
| 77 | See C<TODO> file | ||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||
| 79 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | L<Mojo::JSON::Pointer> | ||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||
| 83 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||
| 85 | Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt> | ||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||
| 87 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||
| 89 | Copyright (C) 1998-2026 D&D Corporation | ||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||
| 91 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||
| 93 | This program is distributed under the terms of the Artistic License Version 2.0 | ||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||
| 95 | See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details | ||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||
| 99 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 100 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | my $args = @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {}; | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 102 | my $self = bless { | ||||||||||||||||||||||||||||||||||
| 103 | data => $args->{data} | ||||||||||||||||||||||||||||||||||
| 104 | 4 | }, $class; | |||||||||||||||||||||||||||||||||
| 105 | 4 | return $self; | |||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||
| 107 | 21 | ▶ | sub data { | ||||||||||||||||||||||||||||||||
|
Subroutine
data: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 108 | 21 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 109 | 21 | ▶ | if (scalar(@_) >= 1) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 110 | 1 | $self->{data} = shift; | |||||||||||||||||||||||||||||||||
| 111 | 1 | return $self; | |||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||
| 113 | 20 | return $self->{data}; | |||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||
| 115 | 5 | ▶ | sub contains { shift->_p(0, @_) } | ||||||||||||||||||||||||||||||||
|
Subroutine
contains: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 116 | 15 | ▶ | sub get { shift->_p(1, @_) } | ||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | 20 | ▶ | sub _p { | ||||||||||||||||||||||||||||||||
|
Subroutine
_p: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||||||||
| 119 | 20 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 120 | 20 | my $get = shift; | |||||||||||||||||||||||||||||||||
| 121 | 20 | ▶ | my $pointer = shift // ''; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() // ""
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 122 | 20 | $pointer =~ s|^/||; | |||||||||||||||||||||||||||||||||
| 123 | 20 | my $data = $self->data; | |||||||||||||||||||||||||||||||||
| 124 | 20 | ▶ | return $get ? $data : 1 unless length($pointer); | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 125 | 18 | ▶ | foreach my $p (length($pointer) ? (split /\//, $pointer, -1) : ($pointer)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 126 | 31 | $p =~ s|~1|/|g; | |||||||||||||||||||||||||||||||||
| 127 | 31 | $p =~ s|~0|~|g; | |||||||||||||||||||||||||||||||||
| 128 | 31 | ▶ | if ((ref($data) eq 'HASH') && exists $data->{$p}) { # Hash ref | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
ref $data eq "HASH" and exists $data->{$p}
MC/DC100%2/2
ref $data eq "HASH" and exists $data->{$p}
ref $data eq "HASH"
exists $data->{$p}
Truth table100%3/3
A and B
A= ref $data eq "HASH"B= exists $data->{$p}
| |||||||||||||||||||||||||||||||||||
| 129 | 26 | $data = $data->{$p} | |||||||||||||||||||||||||||||||||
| 130 | ▶ | } elsif ((ref($data) eq 'ARRAY') && ($p =~ /^[0-9]+$/) && @$data > $p) { # Array ref | |||||||||||||||||||||||||||||||||
|
Condition83%5/6
ref $data eq "ARRAY" and $p =~ /^[0-9]+$/ and @$data > $p
ref $data eq "ARRAY" and $p =~ /^[0-9]+$/
MC/DC66%2/3
ref $data eq "ARRAY" and $p =~ /^[0-9]+$/ and @$data > $p
ref $data eq "ARRAY"
$p =~ /^[0-9]+$/
@$data > $p
Truth table75%3/4
A and B and C
A= ref $data eq "ARRAY"B= $p =~ /^[0-9]+$/C= @$data > $p
| |||||||||||||||||||||||||||||||||||
| 131 | 3 | $data = $data->[$p] | |||||||||||||||||||||||||||||||||
| 132 | } else { # Not found | ||||||||||||||||||||||||||||||||||
| 133 | 2 | return undef; | |||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||
| 136 | 16 | ▶ | return $get ? $data : 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||
| 139 | 2 | 1; | |||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||
| 141 | __END__ | ||||||||||||||||||||||||||||||||||