lib/Template/Plugin/URL.pm

filter: Statementstmt 100% 35 / 35 Branchbran 83.3% 10 / 12 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 7 / 7 Podpod 33.3% 1 / 3 totaltotal 94.6% 71 / 75 CC 12 SCAR 16.7
CC
12
Cov
97%
__ANON__
9.0
escape
2.0
new
2.0
CRAP 5.3
« lib/Template/Directive.pm ↑ Summary lib/Template/Constants.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.100';
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 sub new {
38 13 ▶ my ($class, $context, $base, $args) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
39 13 ▶ $args ||= { };
Condition100%2/2
$args ||= {}
l!l
85
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
40  
41 return sub {
42 28 ▶ my $newbase = shift unless ref $_[0] eq 'HASH';
Subroutine __ANON__: called CC=9 SCAR=22.0
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 17 11 28
43 28 ▶ my $newargs = shift || { };
Condition100%2/2
shift() || {}
l!l
1414
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
44 28 my $combo = { %$args, %$newargs };
45 my $urlargs = join(
46 $JOINT,
47 33 map { args($_, $combo->{ $_ }) }
48 28 ▶ grep { defined $combo->{ $_ } && length $combo->{ $_ } }
Branch: 1 branch
BranchTrueFalseTotal
defined $combo->{$_} and length $combo->{$_} 33 0 33
49 sort keys %$combo
50 );
51  
52 28 ▶ my $query = $newbase || $base || '';
Condition100%5/5
$newbase || $base || ''
l!l
262
$newbase or $base
l!l&&r!l&&!r
5212
MC/DC100%2/2
$newbase || $base || ''
$newbase $base
Truth table100%3/3
A or B
A= $newbaseB= $base
ABresult
1X1
011
000
53 28 ▶ $query .= '?' if length $query && length $urlargs;
Branch: 1 branch
BranchTrueFalseTotal
if length $query and length $urlargs 22 6 28
Condition100%3/3
length $query and length $urlargs
!ll&&!rl&&r
2422
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 28 ▶ $query .= $urlargs if length $urlargs;
Branch: 1 branch
BranchTrueFalseTotal
if length $urlargs 23 5 28
55  
56 28 return $query
57 }
58 13 }
59  
60  
61 sub args {
62 33 ▶ my ($key, $val) = @_;
Subroutine args: called CC=2 SCAR=6.9
Pod undocumented
63 33 $key = escape($key);
64  
65 return map {
66 33 ▶ "$key=" . escape($_);
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 2 31 33
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 sub escape {
79 69 ▶ my $toencode = shift;
Subroutine escape: called CC=2 SCAR=7.0
Pod undocumented
80 69 ▶ return undef unless defined($toencode);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $toencode 0 69 69
81 69 utf8::encode($toencode);
82 69 $toencode=~s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
83 69 return $toencode;
84 }
85  
86 3 1;
87  
88 __END__
« lib/Template/Directive.pm ↑ Summary lib/Template/Constants.pm »