| 1 | package Data::DPath::Path; | ||||||||||||||||||||||||||||||||||||||||
| 2 | 15 | our $AUTHORITY = 'cpan:SCHWIGON'; | |||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Abstraction for a DPath | ||||||||||||||||||||||||||||||||||||||||
| 4 | 15 | $Data::DPath::Path::VERSION = '0.60'; | |||||||||||||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use aliased 'Data::DPath::Step'; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 10 | 15 | ▶ | use aliased 'Data::DPath::Point'; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 11 | 15 | ▶ | use aliased 'Data::DPath::Context'; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 12 | 15 | ▶ | use Text::Balanced 2.02 'extract_delimited', 'extract_codeblock'; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||
| 14 | use Class::XSAccessor | ||||||||||||||||||||||||||||||||||||||||
| 15 | 15 | ▶ | chained => 1, | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 16 | accessors => { | ||||||||||||||||||||||||||||||||||||||||
| 17 | path => 'path', | ||||||||||||||||||||||||||||||||||||||||
| 18 | _steps => '_steps', | ||||||||||||||||||||||||||||||||||||||||
| 19 | give_references => 'give_references', | ||||||||||||||||||||||||||||||||||||||||
| 20 | 15 | }; | |||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||
| 22 | 15 | ▶ | use constant { ROOT => 'ROOT', | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 23 | ANYWHERE => 'ANYWHERE', | ||||||||||||||||||||||||||||||||||||||||
| 24 | KEY => 'KEY', | ||||||||||||||||||||||||||||||||||||||||
| 25 | ANYSTEP => 'ANYSTEP', | ||||||||||||||||||||||||||||||||||||||||
| 26 | NOSTEP => 'NOSTEP', | ||||||||||||||||||||||||||||||||||||||||
| 27 | PARENT => 'PARENT', | ||||||||||||||||||||||||||||||||||||||||
| 28 | ANCESTOR => 'ANCESTOR', | ||||||||||||||||||||||||||||||||||||||||
| 29 | ANCESTOR_OR_SELF => 'ANCESTOR_OR_SELF', | ||||||||||||||||||||||||||||||||||||||||
| 30 | 15 | }; | |||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||
| 32 | 447 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 33 | 447 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||
| 34 | 447 | my $self = bless { @_ }, $class; | |||||||||||||||||||||||||||||||||||||||
| 35 | 447 | $self->_build__steps; | |||||||||||||||||||||||||||||||||||||||
| 36 | 447 | return $self; | |||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||
| 39 | 1784 | ▶ | sub unescape { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
unescape: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 40 | 1784 | my ($str) = @_; | |||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||
| 42 | 1784 | ▶ | return unless defined $str; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 43 | 1784 | $str =~ s/\\{2}/\\/g; | |||||||||||||||||||||||||||||||||||||||
| 44 | 1784 | $str =~ s/\\(["'])/$1/g; # '"$ | |||||||||||||||||||||||||||||||||||||||
| 45 | 1784 | return $str; | |||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||
| 48 | 39 | ▶ | sub unquote { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
unquote: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 49 | 39 | my ($str) = @_; | |||||||||||||||||||||||||||||||||||||||
| 50 | 39 | $str =~ s/^"(.*)"$/$1/g; | |||||||||||||||||||||||||||||||||||||||
| 51 | 39 | return $str; | |||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||
| 54 | 1779 | ▶ | sub quoted { shift =~ m,^/["'],; } # " | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
quoted: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||
| 56 | 15 | ▶ | eval 'use overload "~~" => \&op_match' if $] >= 5.010 and $] < 5.041003; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$] >= "5.01" and $] < "5.041003"
MC/DC0%0/2
$] >= "5.01" and $] < "5.041003"
$] >= "5.01"
$] < "5.041003"
Truth table33%1/3
A and B
A= $] >= "5.01"B= $] < "5.041003"
| |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub op_match { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
op_match: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | my ($self, $data, $rhs) = @_; | |||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | return $self->matchr( $data ); | |||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||
| 64 | # essentially the Path parser | ||||||||||||||||||||||||||||||||||||||||
| 65 | 447 | ▶ | sub _build__steps { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build__steps: called CC=21 SCAR=30.4
| |||||||||||||||||||||||||||||||||||||||||
| 66 | 447 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||
| 68 | 447 | my $remaining_path = $self->path; | |||||||||||||||||||||||||||||||||||||||
| 69 | 447 | my $extracted; | |||||||||||||||||||||||||||||||||||||||
| 70 | my @steps; | ||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||
| 72 | 447 | push @steps, Step->new->part('')->kind(ROOT); | |||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||
| 74 | 447 | ▶ | while ($remaining_path) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 75 | 1779 | my $plain_part; | |||||||||||||||||||||||||||||||||||||||
| 76 | my $filter; | ||||||||||||||||||||||||||||||||||||||||
| 77 | 1779 | my $kind; | |||||||||||||||||||||||||||||||||||||||
| 78 | 1779 | ▶ | if ( quoted($remaining_path) ) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 79 | 39 | ($plain_part, $remaining_path) = extract_delimited($remaining_path, q/'"/, "/"); # ' | |||||||||||||||||||||||||||||||||||||||
| 80 | 39 | ($filter, $remaining_path) = extract_codeblock($remaining_path, "[]"); | |||||||||||||||||||||||||||||||||||||||
| 81 | 39 | $plain_part = unescape unquote $plain_part; | |||||||||||||||||||||||||||||||||||||||
| 82 | 39 | $kind = KEY; # quoted is always a key | |||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||
| 84 | else | ||||||||||||||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||||||||||||||
| 86 | 1740 | my $filter_already_extracted = 0; | |||||||||||||||||||||||||||||||||||||||
| 87 | 1740 | ($extracted, $remaining_path) = extract_delimited($remaining_path,'/'); | |||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||
| 89 | 1740 | ▶ | if (not $extracted) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 90 | 430 | ($extracted, $remaining_path) = ($remaining_path, undef); # END OF PATH | |||||||||||||||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||
| 93 | # work around to recognize slashes in filter expressions and handle them: | ||||||||||||||||||||||||||||||||||||||||
| 94 | # | ||||||||||||||||||||||||||||||||||||||||
| 95 | # - 1) see if key unexpectedly contains opening "[" but no closing "]" | ||||||||||||||||||||||||||||||||||||||||
| 96 | # - 2) use the part before "[" | ||||||||||||||||||||||||||||||||||||||||
| 97 | # - 3) unshift the rest to remaining | ||||||||||||||||||||||||||||||||||||||||
| 98 | # - 4) extract_codeblock() explicitely | ||||||||||||||||||||||||||||||||||||||||
| 99 | 1310 | ▶ | if ($extracted =~ /(.*)((?<!\\)\[.*)/ and $extracted !~ m|\]/\s*$|) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$extracted =~ /(.*)((?<!\\)\[.*)/ and not $extracted =~ m(\]/\s*$)
MC/DC100%2/2
$extracted =~ /(.*)((?<!\\)\[.*)/ and not $extracted =~ m(\]/\s*$)
$extracted =~ /(.*)((?<!\\)\[.*)/
not $extracted =~ m(\]/\s*$)
Truth table100%3/3
A and not(B)
A= $extracted =~ /(.*)((?<!\\)\[.*)/B= not $extracted =~ m(\]/\s*$)
| |||||||||||||||||||||||||||||||||||||||||
| 100 | 32 | $remaining_path = $2 . $remaining_path; | |||||||||||||||||||||||||||||||||||||||
| 101 | 32 | ( $plain_part = $1 ) =~ s|^/||; | |||||||||||||||||||||||||||||||||||||||
| 102 | 32 | ($filter, $remaining_path) = extract_codeblock($remaining_path, "[]"); | |||||||||||||||||||||||||||||||||||||||
| 103 | 32 | $filter_already_extracted = 1; | |||||||||||||||||||||||||||||||||||||||
| 104 | } else { | ||||||||||||||||||||||||||||||||||||||||
| 105 | 1278 | $remaining_path = (chop $extracted) . $remaining_path; | |||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||
| 109 | 1740 | ▶ | ($plain_part, $filter) = $extracted =~ m,^/ # leading / | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 110 | (.*?) # path part | ||||||||||||||||||||||||||||||||||||||||
| 111 | (\[.*\])?$ # optional filter | ||||||||||||||||||||||||||||||||||||||||
| 112 | ,xsg unless $filter_already_extracted; | ||||||||||||||||||||||||||||||||||||||||
| 113 | 1740 | $plain_part = unescape $plain_part; | |||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||
| 116 | 15 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 117 | 1779 | ▶ | if ($plain_part eq '') { $kind ||= ANYWHERE } | ||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
Condition100%2/2
$kind ||= "ANYWHERE"
MC/DC100%1/1
$kind ||= "ANYWHERE"
$kind
Truth table100%2/2
A
A= $kind
| |||||||||||||||||||||||||||||||||||||||||
| 118 | 293 | ▶ | elsif ($plain_part eq '*') { $kind ||= ANYSTEP } | ||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$kind ||= "ANYSTEP"
MC/DC100%1/1
$kind ||= "ANYSTEP"
$kind
Truth table100%2/2
A
A= $kind
| |||||||||||||||||||||||||||||||||||||||||
| 119 | 125 | ▶ | elsif ($plain_part eq '.') { $kind ||= NOSTEP } | ||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$kind ||= "NOSTEP"
MC/DC100%1/1
$kind ||= "NOSTEP"
$kind
Truth table100%2/2
A
A= $kind
| |||||||||||||||||||||||||||||||||||||||||
| 120 | 107 | ▶ | elsif ($plain_part eq '..') { $kind ||= PARENT } | ||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$kind ||= "PARENT"
MC/DC100%1/1
$kind ||= "PARENT"
$kind
Truth table100%2/2
A
A= $kind
| |||||||||||||||||||||||||||||||||||||||||
| 121 | 19 | ▶ | elsif ($plain_part eq '::ancestor') { $kind ||= ANCESTOR } | ||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$kind ||= "ANCESTOR"
MC/DC0%0/1
$kind ||= "ANCESTOR"
$kind
Truth table50%1/2
A
A= $kind
| |||||||||||||||||||||||||||||||||||||||||
| 122 | 21 | ▶ | elsif ($plain_part eq '::ancestor-or-self') { $kind ||= ANCESTOR_OR_SELF } | ||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$kind ||= "ANCESTOR_OR_SELF"
MC/DC100%1/1
$kind ||= "ANCESTOR_OR_SELF"
$kind
Truth table100%2/2
A
A= $kind
| |||||||||||||||||||||||||||||||||||||||||
| 123 | 919 | ▶ | else { $kind ||= KEY } | ||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$kind ||= "KEY"
MC/DC100%1/1
$kind ||= "KEY"
$kind
Truth table100%2/2
A
A= $kind
| |||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||
| 125 | 1779 | push @steps, Step->new->part($plain_part)->kind($kind)->filter($filter); | |||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||
| 127 | 447 | ▶ | pop @steps if $steps[-1]->kind eq ANYWHERE; # ignore final '/' | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 128 | 447 | $self->_steps( \@steps ); | |||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||
| 131 | 387 | ▶ | sub match { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
match: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 132 | 387 | my ($self, $data) = @_; | |||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||
| 134 | 387 | return @{$self->matchr($data)}; | |||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||
| 137 | 423 | ▶ | sub matchr { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
matchr: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 138 | 423 | my ($self, $data) = @_; | |||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||
| 140 | 423 | my $context = Context | |||||||||||||||||||||||||||||||||||||||
| 141 | ->new | ||||||||||||||||||||||||||||||||||||||||
| 142 | ->current_points([ Point->new->ref(\$data) ]) | ||||||||||||||||||||||||||||||||||||||||
| 143 | ->give_references($self->give_references); | ||||||||||||||||||||||||||||||||||||||||
| 144 | 423 | return $context->matchr($self); | |||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||
| 147 | 15 | 1; | |||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||||||||||||||