| 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 = '&'; | |||||||||||||||||||||||||||
| 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 ||= {}
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 43 | 29 | ▶ | my $newargs = shift || { }; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 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 || ''
$newbase or $base
MC/DC100%2/2
$newbase || $base || ''
$newbase
$base
Truth table100%3/3
A or B
A= $newbaseB= $base
| |||||||||||||||||||||||||||||
| 53 | 29 | ▶ | $query .= '?' if length $query && length $urlargs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $query and length $urlargs
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
| |||||||||||||||||||||||||||||
| 54 | 29 | ▶ | $query .= $urlargs if length $urlargs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||