Acrux/Pointer.pm

filter: Statementstmt 96.9% 32 / 33 Branchbran 88.8% 16 / 18 Conditioncond 90.9% 10 / 11 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 7 / 7 Podpod 100% 4 / 4 totaltotal 93.6% 74 / 79 CC 15 SCAR 20.4
CC
15
Cov
94%
_p
12.0
new
3.1
data
2.0
CRAP 7.7
« Acrux/RefUtil.pm ↑ Summary Acrux/FilePid.pm »
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
BranchTrueFalseTotal
@_ ? : 3 1 4
@_ > 1 ? : 3 0 3
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
BranchTrueFalseTotal
if (scalar @_ >= 1) 1 20 21
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() // ""
l!l
182
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
122 20 $pointer =~ s|^/||;
123 20 my $data = $self->data;
124 20 ▶ return $get ? $data : 1 unless length($pointer);
Branch: 2 branches
BranchTrueFalseTotal
unless length $pointer 2 18 20
$get ? : 1 1 2
125 18 ▶ foreach my $p (length($pointer) ? (split /\//, $pointer, -1) : ($pointer)) {
Branch: 1 branch
BranchTrueFalseTotal
length $pointer ? : 18 0 18
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
BranchTrueFalseTotal
if (ref $data eq 'HASH' and exists $data->{$p}) { } 26 5 31
elsif (ref $data eq 'ARRAY' and $p =~ /^[0-9]+$/ and @$data > $p) { } 3 2 5
Condition100%3/3
ref $data eq "HASH" and exists $data->{$p}
!ll&&!rl&&r
4126
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}
ABresult
0X0
100
111
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
!ll&&!rl&&r
113
ref $data eq "ARRAY" and $p =~ /^[0-9]+$/
!ll&&!rl&&r
104
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
ABCresult
0XX0
10X0
1100
1111
131 3 $data = $data->[$p]
132 } else { # Not found
133 2 return undef;
134 }
135 }
136 16 ▶ return $get ? $data : 1;
Branch: 1 branch
BranchTrueFalseTotal
$get ? : 14 2 16
137 }
138  
139 2 1;
140  
141 __END__
« Acrux/RefUtil.pm ↑ Summary Acrux/FilePid.pm »