blib/lib/URI/Template.pm

filter: Statementstmt 59.6% 108 / 181 Branchbran 32.3% 33 / 102 Conditioncond 25.0% 5 / 20 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 82.6% 19 / 23 Podpod 87.5% 7 / 8 totaltotal 49.8% 173 / 347 CC 63 SCAR 37.4
CC
63
Cov
48%
_tostring_query
132.0
_tostring_semi
90.0
_tostring_path
90.0
CRAP 42.3
↑ Summary
1 package URI::Template;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = '0.24';
7  
8 4 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Unicode::Normalize ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use overload '""' => \&template;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 our @EXPORT = qw ( );
16  
17 4 our @EXPORT_OK = qw (
18 template_process
19 template_process_to_string
20 );
21  
22 4 our %EXPORT_TAGS = (
23 'all' => \@EXPORT_OK,
24 );
25  
26 4 my $RESERVED = q(:/?#\[\]\@!\$\&'\(\)\*\+,;=);
27 4 my %TOSTRING = (
28 '' => \&_tostring,
29 '+' => \&_tostring,
30 '#' => \&_tostring,
31 ';' => \&_tostring_semi,
32 '?' => \&_tostring_query,
33 '&' => \&_tostring_query,
34 '/' => \&_tostring_path,
35 '.' => \&_tostring_path,
36 );
37  
38 10 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
39 10 my $class = shift;
40 10 my $templ = shift;
41 10 ▶ $templ = '' unless defined $templ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $templ 2 8 10
42 10 my $self = bless { template => $templ, _vars => {} } => $class;
43  
44 10 $self->_study;
45  
46 10 return $self;
47 }
48  
49 16 ▶sub _quote {
Subroutine _quote: called CC=4 SCAR=17.9
50 16 my ( $val, $safe ) = @_;
51 16 ▶ $safe ||= '';
Condition50%1/2
$safe ||= ""
l!l
016
MC/DC0%0/1
$safe ||= ""
$safe
Truth table50%1/2
A
A= $safe
Aresult
11
00
52 16 my $unsafe = '^A-Za-z0-9\-\._' . $safe;
53  
54 ## Where RESERVED are allowed to pass-through, so are
55 ## already-pct-encoded values
56 16 ▶ if( $safe ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($safe) 0 16 16
57 0 my (@chunks) = split(/(%[0-9A-Fa-f]{2})/, $val);
58  
59 # even chunks are not %xx, odd chunks are
60 return join '',
61 0 ▶ map { $_ % 2
Branch: 1 branch
BranchTrueFalseTotal
$_ % 2 ? : 0 0 0
62 ? $chunks[$_]
63 : URI::Escape::uri_escape_utf8( Unicode::Normalize::NFKC($chunks[$_]), $unsafe ) } 0..$#chunks;
64  
65 }
66  
67 # try to mirror python's urllib quote
68 16 return URI::Escape::uri_escape_utf8( Unicode::Normalize::NFKC( $val ),
69 $unsafe );
70 }
71  
72 16 ▶sub _tostring {
Subroutine _tostring: called CC=6 SCAR=26.2
73 16 my ( $var, $value, $exp ) = @_;
74 16 my $safe = $exp->{ safe };
75  
76 16 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 16 16
elsif (ref $value eq 'HASH') { } 0 16 16
elsif (defined $value) { } 16 0 16
77 0 return join( ',', map { _quote( $_, $safe ) } @$value );
78 }
79 elsif ( ref $value eq 'HASH' ) {
80 return join(
81 ',',
82 map {
83 0 _quote( $_, $safe )
84 . ( $var->{ explode } ? '=' : ',' )
85 0 ▶ . _quote( $value->{ $_ }, $safe )
Branch: 1 branch
BranchTrueFalseTotal
$var->{'explode'} ? : 0 0 0
86 } sort keys %$value
87 );
88 }
89 elsif ( defined $value ) {
90 return _quote(
91 16 ▶ substr( $value, 0, $var->{ prefix } || length( $value ) ),
Condition33%1/3
$var->{'prefix'} || length $value
l!l&&r!l&&!r
0160
MC/DC0%0/2
$var->{'prefix'} || length $value
$var->{'prefix'} length $value
Truth table33%1/3
A || B
A= $var->{'prefix'}B= length $value
ABresult
1X1
011
000
92 $safe );
93 }
94  
95 0 return;
96 }
97  
98 0 ▶sub _tostring_semi {
Subroutine _tostring_semi: not called CC=9 SCAR=45.0
99 0 my ( $var, $value, $exp ) = @_;
100 0 my $safe = $exp->{ safe };
101 0 my $join = $exp->{ op };
102 0 ▶ $join = '&' if $exp->{ op } eq '?';
Branch: 1 branch
BranchTrueFalseTotal
if $exp->{"op"} eq "?" 0 0 0
103  
104 0 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
elsif (ref $value eq 'HASH') { } 0 0 0
elsif (defined $value) { } 0 0 0
105 0 ▶ if ( $var->{ explode } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->{'explode'}) { } 0 0 0
106 return join( $join,
107 0 map { $var->{ name } . '=' . _quote( $_, $safe ) } @$value );
108 }
109 else {
110 return $var->{ name } . '='
111 0 . join( ',', map { _quote( $_, $safe ) } @$value );
112 }
113 }
114 elsif ( ref $value eq 'HASH' ) {
115 0 ▶ if ( $var->{ explode } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->{'explode'}) { } 0 0 0
116 return join(
117 $join,
118 map {
119 0 _quote( $_, $safe ) . '='
120 0 . _quote( $value->{ $_ }, $safe )
121 } sort keys %$value
122 );
123 }
124 else {
125 return $var->{ name } . '=' . join(
126 ',',
127 map {
128 0 _quote( $_, $safe ) . ','
129 0 . _quote( $value->{ $_ }, $safe )
130 } sort keys %$value
131 );
132 }
133 }
134 elsif ( defined $value ) {
135 0 ▶ return $var->{ name } unless length( $value );
Branch: 1 branch
BranchTrueFalseTotal
unless length $value 0 0 0
136 return
137 $var->{ name } . '='
138 . _quote(
139 0 ▶ substr( $value, 0, $var->{ prefix } || length( $value ) ),
Condition0%0/3
$var->{'prefix'} || length $value
l!l&&r!l&&!r
000
MC/DC0%0/2
$var->{'prefix'} || length $value
$var->{'prefix'} length $value
Truth table0%0/3
A || B
A= $var->{'prefix'}B= length $value
ABresult
1X1
011
000
140 $safe );
141 }
142  
143 0 return;
144 }
145  
146 0 ▶sub _tostring_query {
Subroutine _tostring_query: not called CC=11 SCAR=48.8
147 0 my ( $var, $value, $exp ) = @_;
148 0 my $safe = $exp->{ safe };
149 0 my $join = $exp->{ op };
150 0 ▶ $join = '&' if $exp->{ op } =~ /[?&]/;
Branch: 1 branch
BranchTrueFalseTotal
if $exp->{"op"} =~ /[?&]/ 0 0 0
151  
152 0 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
elsif (ref $value eq 'HASH') { } 0 0 0
elsif (defined $value) { } 0 0 0
153 0 ▶ return if !@$value;
Branch: 1 branch
BranchTrueFalseTotal
unless @$value 0 0 0
154 0 ▶ if ( $var->{ explode } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->{'explode'}) { } 0 0 0
155 return join( $join,
156 0 map { $var->{ name } . '=' . _quote( $_, $safe ) } @$value );
157 }
158 else {
159 return $var->{ name } . '='
160 0 . join( ',', map { _quote( $_, $safe ) } @$value );
161 }
162 }
163 elsif ( ref $value eq 'HASH' ) {
164 0 ▶ return if !keys %$value;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$value 0 0 0
165 0 ▶ if ( $var->{ explode } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->{'explode'}) { } 0 0 0
166 return join(
167 $join,
168 map {
169 0 _quote( $_, $safe ) . '='
170 0 . _quote( $value->{ $_ }, $safe )
171 } sort keys %$value
172 );
173 }
174 else {
175 return $var->{ name } . '=' . join(
176 ',',
177 map {
178 0 _quote( $_, $safe ) . ','
179 0 . _quote( $value->{ $_ }, $safe )
180 } sort keys %$value
181 );
182 }
183 }
184 elsif ( defined $value ) {
185 0 ▶ return $var->{ name } . '=' unless length( $value );
Branch: 1 branch
BranchTrueFalseTotal
unless length $value 0 0 0
186 return
187 $var->{ name } . '='
188 . _quote(
189 0 ▶ substr( $value, 0, $var->{ prefix } || length( $value ) ),
Condition0%0/3
$var->{'prefix'} || length $value
l!l&&r!l&&!r
000
MC/DC0%0/2
$var->{'prefix'} || length $value
$var->{'prefix'} length $value
Truth table0%0/3
A || B
A= $var->{'prefix'}B= length $value
ABresult
1X1
011
000
190 $safe );
191 }
192 }
193  
194 0 ▶sub _tostring_path {
Subroutine _tostring_path: not called CC=9 SCAR=45.0
195 0 my ( $var, $value, $exp ) = @_;
196 0 my $safe = $exp->{ safe };
197 0 my $join = $exp->{ op };
198  
199 0 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
elsif (ref $value eq 'HASH') { } 0 0 0
elsif (defined $value) { } 0 0 0
200 0 ▶ return unless @$value;
Branch: 1 branch
BranchTrueFalseTotal
unless @$value 0 0 0
201 return join(
202 ( $var->{ explode } ? $join : ',' ),
203 0 ▶ map { _quote( $_, $safe ) } @$value
Branch: 1 branch
BranchTrueFalseTotal
$var->{'explode'} ? : 0 0 0
204 );
205 }
206 elsif ( ref $value eq 'HASH' ) {
207 return join(
208 ( $var->{ explode } ? $join : ',' ),
209 map {
210 0 ▶ _quote( $_, $safe )
Branch: 1 branch
BranchTrueFalseTotal
$var->{'explode'} ? : 0 0 0
211 . ( $var->{ explode } ? '=' : ',' )
212 0 ▶ . _quote( $value->{ $_ }, $safe )
Branch: 1 branch
BranchTrueFalseTotal
$var->{'explode'} ? : 0 0 0
213 } sort keys %$value
214 );
215 }
216 elsif ( defined $value ) {
217 return _quote(
218 0 ▶ substr( $value, 0, $var->{ prefix } || length( $value ) ),
Condition0%0/3
$var->{'prefix'} || length $value
l!l&&r!l&&!r
000
MC/DC0%0/2
$var->{'prefix'} || length $value
$var->{'prefix'} length $value
Truth table0%0/3
A || B
A= $var->{'prefix'}B= length $value
ABresult
1X1
011
000
219 $safe );
220 }
221  
222 0 return;
223 }
224  
225 12 ▶sub _study {
Subroutine _study: called CC=4 SCAR=14.0
226 12 my ( $self ) = @_;
227 12 ▶ my @hunks = grep { defined && length } split /(\{.+?\})/, $self->template;
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
0038
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
228 12 my $pos = 1;
229 12 for ( @hunks ) {
230 38 ▶ next unless /^\{(.+?)\}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\{(.+?)\}$/ 22 16 38
231 16 $_ = $self->_compile_expansion( $1, $pos++ );
232 }
233 12 $self->{ studied } = \@hunks;
234 }
235  
236 16 ▶sub _compile_expansion {
Subroutine _compile_expansion: called CC=14 SCAR=31.0
237 16 my ( $self, $str, $pos ) = @_;
238  
239 16 my %exp = ( op => '', vars => [], str => $str );
240 16 ▶ if ( $str =~ /^([+#.\/;?&|!\@])(.+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ m[^([+#./;?&|!\@])(.+)]) 0 16 16
241 0 $exp{ op } = $1;
242 0 $exp{ str } = $2;
243 }
244  
245 16 ▶ $exp{ safe } = $RESERVED if $exp{ op } =~ m{[+#]};
Branch: 1 branch
BranchTrueFalseTotal
if $exp{"op"} =~ /[+#]/ 0 16 16
246  
247 16 for my $varspec ( split( ',', delete $exp{ str } ) ) {
248 16 my %var = ( name => $varspec );
249 16 ▶ if ( $varspec =~ /=/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($varspec =~ /=/) 0 16 16
250 0 @var{ 'name', 'default' } = split( /=/, $varspec, 2 );
251 }
252 16 ▶ if ( $var{ name } =~ s{\*$}{} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($var{'name'} =~ s/\*$//) { } 0 16 16
elsif ($var{'name'} =~ /:/) { } 0 16 16
253 0 $var{ explode } = 1;
254 }
255 elsif ( $var{ name } =~ /:/ ) {
256 0 @var{ 'name', 'prefix' } = split( /:/, $var{ name }, 2 );
257 0 ▶ if ( $var{ prefix } =~ m{[^0-9]} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var{"prefix"} =~ /[^0-9]/) 0 0 0
258 0 die 'Non-numeric prefix specified';
259 }
260 }
261  
262 # remove "optional" flag (for opensearch compatibility)
263 16 $var{ name } =~ s{\?$}{};
264 16 $self->{ _vars }->{ $var{ name } } = $pos;
265  
266 16 push @{ $exp{ vars } }, \%var;
267 }
268  
269 16 my $join = $exp{ op };
270 16 my $start = $exp{ op };
271  
272 16 ▶ if ( $exp{ op } eq '+' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($exp{'op'} eq '+') { } 0 16 16
elsif ($exp{'op'} eq '#') { } 0 16 16
elsif ($exp{'op'} eq '?') { } 0 16 16
elsif ($exp{'op'} eq '&') { } 0 16 16
elsif ($exp{'op'} eq '') { } 16 0 16
273 0 $start = '';
274 0 $join = ',';
275 }
276 elsif ( $exp{ op } eq '#' ) {
277 0 $join = ',';
278 }
279 elsif ( $exp{ op } eq '?' ) {
280 0 $join = '&';
281 }
282 elsif ( $exp{ op } eq '&' ) {
283 0 $join = '&';
284 }
285 elsif ( $exp{ op } eq '' ) {
286 16 $join = ',';
287 }
288  
289 16 ▶ if ( !exists $TOSTRING{ $exp{ op } } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $TOSTRING{$exp{"op"}}) 0 16 16
290 0 die 'Invalid operation "' . $exp{ op } . '"';
291 }
292  
293 18 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
294 18 my $variables = shift;
295  
296 18 my @return;
297 18 for my $var ( @{ $exp{ vars } } ) {
298 18 my $value;
299 18 ▶ if ( exists $variables->{ $var->{ name } } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$variables{$var->{"name"}}) 16 2 18
300 16 $value = $variables->{ $var->{ name } };
301 }
302 18 ▶ $value = $var->{ default } if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 16 18
303  
304 18 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 16 18
305  
306 16 my $expand = $TOSTRING{ $exp{ op } }->( $var, $value, \%exp );
307  
308 16 ▶ push @return, $expand if defined $expand;
Branch: 1 branch
BranchTrueFalseTotal
if defined $expand 16 0 16
309 }
310  
311 18 ▶ return $start . join( $join, @return ) if @return;
Branch: 1 branch
BranchTrueFalseTotal
if @return 16 2 18
312 2 return '';
313 16 };
314 }
315  
316 19 ▶sub template {
Subroutine template: called CC=3 SCAR=11.0
Pod documented
317 19 my $self = shift;
318 19 my $templ = shift;
319  
320 # Update template
321 19 ▶ if ( defined $templ && $templ ne $self->{ template } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $templ and $templ ne $self->{"template"}) 2 17 19
Condition66%2/3
defined $templ and $templ ne $self->{"template"}
!ll&&!rl&&r
1702
MC/DC50%1/2
defined $templ and $templ ne $self->{"template"}
defined $templ $templ ne $self->{"template"}
Truth table66%2/3
A and B
A= defined $templB= $templ ne $self->{"template"}
ABresult
0X0
100
111
322 2 $self->{ template } = $templ;
323 2 $self->{ _vars } = {};
324 2 $self->_study;
325 2 return $self;
326 }
327  
328 17 return $self->{ template };
329 }
330  
331 4 ▶sub variables {
Subroutine variables: called CC=1 SCAR=0.0
Pod documented
332 4 my @vars = sort {$_[ 0 ]->{ _vars }->{ $a } <=> $_[ 0 ]->{ _vars }->{ $b } } keys %{ $_[ 0 ]->{ _vars } };
333 4 return @vars;
334 }
335  
336 0 ▶sub expansions {
Subroutine expansions: not called CC=1 SCAR=6.9
Pod documented
337 0 my $self = shift;
338 0 return grep { ref } @{ $self->{ studied } };
339 }
340  
341 11 ▶sub process {
Subroutine process: called CC=1 SCAR=0.0
Pod documented
342 11 my $self = shift;
343 11 return URI->new( $self->process_to_string( @_ ) );
344 }
345  
346 13 ▶sub process_to_string {
Subroutine process_to_string: called CC=4 SCAR=13.9
Pod documented
347 13 my $self = shift;
348 13 ▶ my $arg = @_ == 1 ? $_[ 0 ] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 13 13
349 13 my $str = '';
350  
351 13 for my $hunk ( @{ $self->{ studied } } ) {
352 44 ▶ if ( !ref $hunk ) { $str .= $hunk; next; }
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $hunk) 26 18 44
353  
354 18 $str .= $hunk->( $arg );
355 }
356  
357 13 return $str;
358 }
359  
360 1 ▶sub template_process {
Subroutine template_process: called CC=1 SCAR=0.0
Pod documented
361 1 __PACKAGE__->new(shift)->process(@_)
362 }
363  
364 1 ▶sub template_process_to_string {
Subroutine template_process_to_string: called CC=1 SCAR=0.0
Pod undocumented
365 1 __PACKAGE__->new(shift)->process_to_string(@_)
366 }
367  
368 4 1;
369  
370 __END__
↑ Summary