lib/Template/Plugin/URL.pm

filter: Statementstmt 100% 35 / 35 Branchbran 90.0% 9 / 10 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 7 / 7 Podpod 33.3% 1 / 3 totaltotal 95.8% 70 / 73 CC 11 SCAR 15.4
CC
11
Cov
98%
__ANON__
8.0
escape
2.0
new
2.0
CRAP 4.6
« lib/Template/Config.pm ↑ Summary lib/Template/Base.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Plugin::URL
4 #
5 # DESCRIPTION
6 # Template Toolkit Plugin for constructing URL's from a base stem
7 # and adaptable parameters.
8 #
9 # AUTHOR
10 # Andy Wardley <abw@wardley.org>
11 #
12 # COPYRIGHT
13 # Copyright (C) 2000-2022 Andy Wardley. All Rights Reserved.
14 #
15 # This module is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
17 #
18 #============================================================================
19  
20 package Template::Plugin::URL;
21  
22 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use base 'Template::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 3 our $VERSION = '3.106';
27 3 our $JOINT = '&amp;';
28  
29  
30 #------------------------------------------------------------------------
31 # new($context, $baseurl, \%url_params)
32 #
33 # Constructor method which returns a sub-routine closure for constructing
34 # complex URL's from a base part and hash of additional parameters.
35 #------------------------------------------------------------------------
36  
37 14 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
38 14 my ($class, $context, $base, $args) = @_;
39 14 ▶ $args ||= { };
Condition100%2/2
$args ||= {}
l!l
86
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
40  
41 29 ▶ return sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
42 29 ▶ my $newbase = shift unless ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 17 12 29
43 29 ▶ my $newargs = shift || { };
Condition100%2/2
shift() || {}
l!l
1514
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
44 29 my $combo = { %$args, %$newargs };
45 my $urlargs = join(
46 $JOINT,
47 36 map { args($_, $combo->{ $_ }) }
48 29 grep { defined $combo->{ $_ } }
49 sort keys %$combo
50 );
51  
52 29 ▶ my $query = $newbase || $base || '';
Condition100%5/5
$newbase || $base || ''
l!l
272
$newbase or $base
l!l&&r!l&&!r
5222
MC/DC100%2/2
$newbase || $base || ''
$newbase $base
Truth table100%3/3
A or B
A= $newbaseB= $base
ABresult
1X1
011
000
53 29 ▶ $query .= '?' if length $query && length $urlargs;
Branch: 1 branch
BranchTrueFalseTotal
if length $query and length $urlargs 23 6 29
Condition100%3/3
length $query and length $urlargs
!ll&&!rl&&r
2423
MC/DC100%2/2
length $query and length $urlargs
length $query length $urlargs
Truth table100%3/3
A and B
A= length $queryB= length $urlargs
ABresult
0X0
100
111
54 29 ▶ $query .= $urlargs if length $urlargs;
Branch: 1 branch
BranchTrueFalseTotal
if length $urlargs 24 5 29
55  
56 29 return $query
57 }
58 14 }
59  
60  
61 36 ▶sub args {
Subroutine args: called CC=2 SCAR=6.9
Pod undocumented
62 36 my ($key, $val) = @_;
63 36 $key = escape($key);
64  
65 return map {
66 36 ▶ "$key=" . escape($_);
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 2 34 36
67 } ref $val eq 'ARRAY' ? @$val : $val;
68  
69 }
70  
71 #------------------------------------------------------------------------
72 # escape($url)
73 #
74 # URL-encode data. Borrowed with minor modifications from CGI.pm.
75 # Kudos to Lincold Stein.
76 #------------------------------------------------------------------------
77  
78 75 ▶sub escape {
Subroutine escape: called CC=2 SCAR=7.0
Pod undocumented
79 75 my $toencode = shift;
80 75 ▶ return undef unless defined($toencode);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $toencode 0 75 75
81 75 utf8::encode($toencode);
82 75 $toencode=~s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
83 75 return $toencode;
84 }
85  
86 3 1;
87  
88 __END__
« lib/Template/Config.pm ↑ Summary lib/Template/Base.pm »