| 1 | package URI::Builder; | ||||||||||||||||||||||||||||||||
| 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 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | URI::Builder - URI objects optimised for manipulation | ||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | my $uri = URI::Builder->new( | ||||||||||||||||||||||||||||||||
| 13 | scheme => 'http', | ||||||||||||||||||||||||||||||||
| 14 | host => 'www.cpan.org', | ||||||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | $uri->path_segments(qw( misc cpan-faq.html )); | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | say $uri->as_string; # http://www.cpan.org/misc/cpan-faq.html | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 0.04 | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 4 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | This class is a close relative of L<URI>, but while that class is optimised | ||||||||||||||||||||||||||||||||
| 32 | for parsing, this is optimised for building up or modifying URIs. To that end | ||||||||||||||||||||||||||||||||
| 33 | objects of this class represent their URIs in sections, each of which are | ||||||||||||||||||||||||||||||||
| 34 | independently mutable, that then need to be serialised to form a string. In | ||||||||||||||||||||||||||||||||
| 35 | contrast, C<URI> uses a fully-formed string internally which must be parsed | ||||||||||||||||||||||||||||||||
| 36 | afresh each time a mutation is performed on it. | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | At the moment only http and https URIs are known to work correctly, support | ||||||||||||||||||||||||||||||||
| 39 | for other schemes may follow later. | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | =cut | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 4 | ▶ | use URI; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 44 | 4 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 45 | 4 | ▶ | use Carp qw( confess ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | # Utility functions | ||||||||||||||||||||||||||||||||
| 48 | 52 | ▶ | sub _flatten { | ||||||||||||||||||||||||||||||
|
Subroutine
_flatten: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 49 | return map { | ||||||||||||||||||||||||||||||||
| 50 | 52 | ▶ | ref $_ eq 'ARRAY' ? _flatten(@$_) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 51 | : ref $_ eq 'HASH' ? _flatten_hash($_) | ||||||||||||||||||||||||||||||||
| 52 | : $_ | ||||||||||||||||||||||||||||||||
| 53 | } @_ = @_; | ||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 5 | ▶ | sub _flatten_hash { | ||||||||||||||||||||||||||||||
|
Subroutine
_flatten_hash: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 57 | 5 | my $hash = shift; | |||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | return map { | ||||||||||||||||||||||||||||||||
| 60 | 5 | my ($k, $v) = ($_, $hash->{$_}); | |||||||||||||||||||||||||||||||
| 61 | 7 | ▶ | $v = '' unless defined $v; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 62 | 7 | map { $k => $_ } _flatten $v | |||||||||||||||||||||||||||||||
| 63 | } keys %$hash; | ||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 4 | ▶ | use overload ('""' => \&as_string, fallback => 1); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | The following attributes relate closely with the URI methods of the same | ||||||||||||||||||||||||||||||||
| 71 | names. | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | =over | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | =item * scheme | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | =item * userinfo | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | =item * host | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | =item * port | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | =item * path_segments | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | =item * query_form | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | =item * query_keywords | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | =item * fragment | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | =back | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | In addition the C<query_separator> attribute defines how C<query_form> fields | ||||||||||||||||||||||||||||||||
| 94 | are joined. It defaults to C<';'> but can be usefully set to '&'. | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | The accessors for these attributes have a similar interface to the L<URI> | ||||||||||||||||||||||||||||||||
| 97 | methods, that is to say that they return old values when new ones are set. | ||||||||||||||||||||||||||||||||
| 98 | Those attributes that take a list of values: C<path_segments>, C<query_form> | ||||||||||||||||||||||||||||||||
| 99 | and C<query_keywords> all return plain lists but can be passed nested array | ||||||||||||||||||||||||||||||||
| 100 | references. | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | =cut | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 4 | my (@uri_fields, %listish, @fields); | |||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | 4 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 107 | # Fields that correspond to methods in URI | ||||||||||||||||||||||||||||||||
| 108 | 4 | @uri_fields = qw( | |||||||||||||||||||||||||||||||
| 109 | scheme | ||||||||||||||||||||||||||||||||
| 110 | userinfo | ||||||||||||||||||||||||||||||||
| 111 | host | ||||||||||||||||||||||||||||||||
| 112 | port | ||||||||||||||||||||||||||||||||
| 113 | path_segments | ||||||||||||||||||||||||||||||||
| 114 | query_form | ||||||||||||||||||||||||||||||||
| 115 | query_keywords | ||||||||||||||||||||||||||||||||
| 116 | fragment | ||||||||||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | # Fields that contain lists of values | ||||||||||||||||||||||||||||||||
| 120 | 4 | %listish = map { $_ => 1 } qw( | |||||||||||||||||||||||||||||||
| 121 | path_segments | ||||||||||||||||||||||||||||||||
| 122 | query_form | ||||||||||||||||||||||||||||||||
| 123 | query_keywords | ||||||||||||||||||||||||||||||||
| 124 | ); | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | # All fields | ||||||||||||||||||||||||||||||||
| 127 | 4 | @fields = ( @uri_fields, qw( query_separator )); | |||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | # Generate accessors for all fields: | ||||||||||||||||||||||||||||||||
| 130 | 4 | for my $field (@fields) { | |||||||||||||||||||||||||||||||
| 131 | 4 | ▶ | my $glob = do { no strict 'refs'; \*$field }; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | 221 | ▶ | *$glob = $listish{$field} ? sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 134 | 221 | my $self = shift; | |||||||||||||||||||||||||||||||
| 135 | 221 | ▶ | my @old = @{ $self->{$field} || []}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{$field} || []
MC/DC100%1/1
$self->{$field} || []
$self->{$field}
Truth table100%2/2
A
A= $self->{$field}
| |||||||||||||||||||||||||||||||||
| 136 | 221 | ▶ | $self->{$field} = [ _flatten @_ ] if @_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 137 | 221 | return @old; | |||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||
| 139 | 248 | ▶ | : sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 140 | 248 | my $self = shift; | |||||||||||||||||||||||||||||||
| 141 | 248 | my $old = $self->{$field}; | |||||||||||||||||||||||||||||||
| 142 | 248 | ▶ | $self->{$field} = shift if @_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 143 | 248 | return $old; | |||||||||||||||||||||||||||||||
| 144 | 36 | ▶ | }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | =head2 new | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | The constructor. | ||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | In addition to the attributes listed above, a C<uri> argument can be passed as | ||||||||||||||||||||||||||||||||
| 155 | a string or a L<URI> object, which will be parsed to popoulate any missing | ||||||||||||||||||||||||||||||||
| 156 | fields. | ||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||
| 158 | # a cpan URL without its path | ||||||||||||||||||||||||||||||||
| 159 | my $uri = URI::Builder->new( | ||||||||||||||||||||||||||||||||
| 160 | uri => 'http://www.cpan.org/SITES.html', | ||||||||||||||||||||||||||||||||
| 161 | path_segments => [], | ||||||||||||||||||||||||||||||||
| 162 | ); | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | Non-attribute arguments that match other methods in the class will cause those | ||||||||||||||||||||||||||||||||
| 165 | methods to be called on the object. This means that what we internally regard | ||||||||||||||||||||||||||||||||
| 166 | as composite attributes can be specified directly in the constructor. | ||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | # Implicitly populate path_segments: | ||||||||||||||||||||||||||||||||
| 169 | my $uri = URI::Builder->new( path => 'relative/path' ); | ||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | Unrecognised arguments cause an exception. | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | =cut | ||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | 34 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=14 SCAR=27.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 176 | 34 | my $class = shift; | |||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | my %opts = @_ == 1 && ref $_[0] eq 'HASH' ? %{ $_[0] } : @_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1
ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | 34 | ▶ | $opts{query_separator} ||= ';'; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"query_separator"} ||= ";"
MC/DC100%1/1
$opts{"query_separator"} ||= ";"
$opts{"query_separator"}
Truth table100%2/2
A
A= $opts{"query_separator"}
| |||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||
| 181 | 34 | ▶ | if (my $uri = $opts{uri}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 182 | 9 | $uri = $class->_inflate_uri($uri); | |||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | 9 | for my $field (@uri_fields) { | |||||||||||||||||||||||||||||||
| 185 | 72 | ▶ | if (!defined $opts{$field} && (my $code = $uri->can($field))) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $opts{$field} and my $code = $uri->can($field)
MC/DC50%1/2
not defined $opts{$field} and my $code = $uri->can($field)
not defined $opts{$field}
my $code = $uri->can($field)
Truth table66%2/3
not(A) and B
A= not defined $opts{$field}B= my $code = $uri->can($field)
| |||||||||||||||||||||||||||||||||
| 186 | $opts{$field} = | ||||||||||||||||||||||||||||||||
| 187 | 69 | ▶ | $listish{$field} ? [ $code->($uri) ] : $code->($uri); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||
| 192 | $_ = [ _flatten $_ ] | ||||||||||||||||||||||||||||||||
| 193 | 34 | ▶ | for grep defined && ref ne 'ARRAY', @opts{ keys %listish }; | ||||||||||||||||||||||||||||||
|
Condition100%3/3
defined $_ && ref $_ ne 'ARRAY'
MC/DC100%2/2
defined $_ && ref $_ ne 'ARRAY'
defined $_
ref $_ ne 'ARRAY'
Truth table100%3/3
A && B
A= defined $_B= ref $_ ne 'ARRAY'
| |||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | # Still no scheme? Default to http | ||||||||||||||||||||||||||||||||
| 196 | # $opts{scheme} ||= 'http'; | ||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | 34 | my $self = bless { map { $_ => $opts{$_} } @fields }, $class; | |||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | 34 | delete @opts{@fields}; | |||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | 34 | for my $field (sort keys %opts) { | |||||||||||||||||||||||||||||||
| 203 | 14 | ▶ | if (my $method = $self->can($field)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 204 | 14 | $method->($self, _flatten delete $opts{$field}); | |||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | 34 | ▶ | if (my @invalid = sort keys %opts) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 209 | 0 | confess "Unrecognised fields in constructor: ", join ', ', @invalid; | |||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||
| 212 | 34 | return $self; | |||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||
| 215 | # Turn various things into URI objects | ||||||||||||||||||||||||||||||||
| 216 | 11 | ▶ | sub _inflate_uri { | ||||||||||||||||||||||||||||||
|
Subroutine
_inflate_uri: called CC=4 SCAR=15.9
| |||||||||||||||||||||||||||||||||
| 217 | 11 | my ($self, $thing) = @_; | |||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||
| 219 | 11 | ▶ | if (blessed $thing) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 220 | 4 | ▶ | if ($thing->isa('URI')) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 221 | 4 | return $thing; | |||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||
| 223 | elsif ($thing->isa(__PACKAGE__)) { | ||||||||||||||||||||||||||||||||
| 224 | 0 | return $thing->uri; | |||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||
| 226 | else { | ||||||||||||||||||||||||||||||||
| 227 | 0 | return URI->new("$thing"); | |||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||
| 230 | else { | ||||||||||||||||||||||||||||||||
| 231 | 7 | return URI->new($thing); | |||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | =head2 abs | ||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||
| 237 | $absolute_uri = $relative_uri->abs($base_uri) | ||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||
| 239 | Returns a new L<URI::Builder> object as an absolute URL based on the given | ||||||||||||||||||||||||||||||||
| 240 | base URI. | ||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||
| 242 | Implemented as a wrapper of L<URI/abs>. | ||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||
| 244 | =cut | ||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||
| 246 | 1 | ▶ | sub abs { | ||||||||||||||||||||||||||||||
|
Subroutine
abs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 247 | 1 | my ($self, $base, @args) = @_; | |||||||||||||||||||||||||||||||
| 248 | 1 | my $class = ref $self; | |||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||
| 250 | 1 | return $class->new(uri => $self->uri->abs($self->_inflate_uri($base), @args)); | |||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||
| 254 | =head2 rel | ||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||
| 256 | $relative_uri = $absolute_uri->rel($base_uri) | ||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | Returns a new L<URI::Builder> object denoting the relative URI compared with | ||||||||||||||||||||||||||||||||
| 259 | the base URI. | ||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||
| 261 | Implemented as a wrapper of L<URI/rel>. | ||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||
| 263 | =cut | ||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||
| 265 | 1 | ▶ | sub rel { | ||||||||||||||||||||||||||||||
|
Subroutine
rel: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 266 | 1 | my ($self, $base) = @_; | |||||||||||||||||||||||||||||||
| 267 | 1 | my $class = ref $self; | |||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||
| 269 | 1 | return $class->new(uri => $self->uri->rel($self->_inflate_uri($base))); | |||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||
| 272 | =head2 clone | ||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | Returns a new object with all attributes copied. | ||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||
| 276 | =cut | ||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||
| 278 | 2 | ▶ | sub clone { | ||||||||||||||||||||||||||||||
|
Subroutine
clone: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 279 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||
| 281 | 2 | my %clone = %$self; | |||||||||||||||||||||||||||||||
| 282 | 2 | for my $list ( keys %listish ) { | |||||||||||||||||||||||||||||||
| 283 | 6 | ▶ | $clone{$list} &&= [ @{ $clone{$list} || [] } ]; | ||||||||||||||||||||||||||||||
|
Condition75%3/4
$clone{$list} &&= [@{$clone{$list} or [];}]
$clone{$list} || []
MC/DC100%1/1
$clone{$list} &&= [@{$clone{$list} or [];}]
$clone{$list}
MC/DC0%0/1
$clone{$list} || []
$clone{$list}
Truth table100%2/2
A
A= $clone{$list}
Truth table50%1/2
A
A= $clone{$list}
| |||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | 2 | return ref($self)->new(%clone); | |||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||
| 289 | =head2 as_string | ||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||
| 291 | Returns the URI described by the object as a string. This is built up from the | ||||||||||||||||||||||||||||||||
| 292 | individual components each time it's called. | ||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||
| 294 | This is also used as the stringification overload. | ||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||
| 296 | =cut | ||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||
| 298 | 44 | ▶ | sub as_string { | ||||||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 299 | 44 | my $self = shift; | |||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||
| 301 | 44 | my @parts; | |||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||
| 303 | 44 | ▶ | if (my $authority = $self->authority) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 304 | 28 | ▶ | if (my $scheme = $self->scheme) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 305 | 22 | push @parts, "$scheme:"; | |||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||
| 308 | 28 | $authority =~ s/:@{[ $self->default_port ]}\z//; | |||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||
| 310 | 28 | push @parts, "//$authority"; | |||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||
| 313 | 44 | ▶ | if (my $path = $self->path) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 314 | 20 | ▶ | $path =~ s{^(?!/)}{/} if @parts; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 315 | 20 | push @parts, $path; | |||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||
| 318 | 44 | ▶ | if (my $query = $self->query) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 319 | 15 | push @parts, "?$query"; | |||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||
| 322 | 44 | ▶ | if (my $fragment = $self->fragment) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 323 | 3 | push @parts, "#$fragment"; | |||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||
| 326 | 44 | return join('', @parts); | |||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||
| 329 | =head2 uri | ||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||
| 331 | Returns a version of this object as a L<URI> object. | ||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||
| 333 | =cut | ||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||
| 335 | 11 | ▶ | sub uri { | ||||||||||||||||||||||||||||||
|
Subroutine
uri: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 336 | 11 | my $self = shift; | |||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||
| 338 | 11 | return URI->new($self->as_string); | |||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||
| 341 | =head2 default_port | ||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||
| 343 | Returns the default port for the current object's scheme. This is obtained | ||||||||||||||||||||||||||||||||
| 344 | from the appropriate L<URI> subclass. See L<URI/default_port>. | ||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||
| 346 | =head2 secure | ||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||
| 348 | Returns true if the current scheme is a secure one, false otherwise. See | ||||||||||||||||||||||||||||||||
| 349 | L<URI/secure>. | ||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||
| 351 | =cut | ||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||
| 353 | 28 | ▶ | sub _implementor { | ||||||||||||||||||||||||||||||
|
Subroutine
_implementor: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 354 | 28 | my $self = shift; | |||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||
| 356 | 28 | ▶ | return URI::implementor($self->scheme || 'http'); | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->scheme || 'http'
MC/DC100%1/1
$self->scheme || 'http'
$self->scheme
Truth table100%2/2
A
A= $self->scheme
| |||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||
| 359 | 28 | ▶ | sub default_port { shift->_implementor->default_port } | ||||||||||||||||||||||||||||||
|
Subroutine
default_port: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 360 | 0 | ▶ | sub secure { shift->_implementor->secure } | ||||||||||||||||||||||||||||||
|
Subroutine
secure: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||
| 362 | =head2 authority | ||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||
| 364 | Returns the 'authority' section of the URI. In our case this is obtained by | ||||||||||||||||||||||||||||||||
| 365 | combining C<userinfo>, C<host> and C<port> together as appropriate. | ||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||
| 367 | Note that this is a read-only operation. | ||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||
| 369 | =cut | ||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||
| 371 | 44 | ▶ | sub authority { | ||||||||||||||||||||||||||||||
|
Subroutine
authority: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 372 | 44 | my $self = shift; | |||||||||||||||||||||||||||||||
| 373 | 44 | my ($user, $host) = ($self->userinfo, $self->host_port); | |||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||
| 375 | 44 | ▶ | return $host ? $user ? "$user\@$host" : $host : ''; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||
| 378 | =head2 host_port | ||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||
| 380 | Returns the host and port in a single string. | ||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||
| 382 | =cut | ||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||
| 384 | 44 | ▶ | sub host_port { | ||||||||||||||||||||||||||||||
|
Subroutine
host_port: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 385 | 44 | my $self = shift; | |||||||||||||||||||||||||||||||
| 386 | 44 | my ($host, $port) = ($self->host, $self->port); | |||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||
| 388 | 44 | ▶ | return $host ? $port ? "$host:$port" : $host : ''; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||
| 391 | =head2 path | ||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||
| 393 | Returns the path portion of the URI as a string. | ||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||
| 395 | Can be assigned to to populate C<path_segments>. | ||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||
| 397 | Leading, trailing and doubled slashes are represented faithfully using empty | ||||||||||||||||||||||||||||||||
| 398 | path segments. | ||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||
| 400 | =cut | ||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||
| 402 | 54 | ▶ | sub path { | ||||||||||||||||||||||||||||||
|
Subroutine
path: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 403 | 54 | my $self = shift; | |||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||
| 405 | 54 | my $old = join '/', $self->path_segments; | |||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||
| 407 | 54 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 408 | 6 | my @segments = split '/', shift, -1; | |||||||||||||||||||||||||||||||
| 409 | 6 | $self->path_segments(@segments); | |||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||
| 412 | 54 | return $old; | |||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||
| 415 | =head2 query | ||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||
| 417 | Returns a string representation of the query. This is obtained from either | ||||||||||||||||||||||||||||||||
| 418 | C<query_form> or C<query_keywords>, in that order. | ||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||
| 420 | If an argument is passed, it is parsed to populate C<query_form>. | ||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||
| 422 | =cut | ||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||
| 424 | 49 | ▶ | sub query { | ||||||||||||||||||||||||||||||
|
Subroutine
query: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 425 | 49 | my ($self, $query) = @_; | |||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||
| 427 | 49 | my @new; | |||||||||||||||||||||||||||||||
| 428 | 49 | ▶ | if ($query) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 429 | # Parse the new query string using a URI object | ||||||||||||||||||||||||||||||||
| 430 | 1 | @new = URI->new("?$query", $self->scheme)->query_form; | |||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||
| 433 | 49 | ▶ | unless (defined wantarray) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 434 | # void context, don't bother building the query string | ||||||||||||||||||||||||||||||||
| 435 | 0 | $self->query_form(@new); | |||||||||||||||||||||||||||||||
| 436 | 0 | return; | |||||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||
| 439 | 49 | my $old; | |||||||||||||||||||||||||||||||
| 440 | 49 | ▶ | if (my @form = $self->query_form) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 441 | 15 | ▶ | push @form, '' if @form % 2; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 442 | 15 | my $uri = URI->new; | |||||||||||||||||||||||||||||||
| 443 | 15 | $uri->query_form(\@form, $self->query_separator); | |||||||||||||||||||||||||||||||
| 444 | 15 | $old = $uri->query(); | |||||||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||||||
| 446 | else { | ||||||||||||||||||||||||||||||||
| 447 | 34 | $old = join '+', $self->query_keywords; | |||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||
| 450 | 49 | $self->query_form(@new); | |||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||
| 452 | 49 | return $old; | |||||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||
| 455 | =head2 path_query | ||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||
| 457 | Returns a string representation of the path plus the query string. See | ||||||||||||||||||||||||||||||||
| 458 | L<URI/path_query>. | ||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||
| 460 | =cut | ||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||
| 462 | 2 | ▶ | sub path_query { | ||||||||||||||||||||||||||||||
|
Subroutine
path_query: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 463 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 464 | 2 | my ($path, $query) = ($self->path, $self->query); | |||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||
| 466 | 2 | ▶ | my $old = $path . ($query ? "?$query" : ''); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||
| 468 | 2 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 469 | 1 | my $uri = URI->new($_[0]); | |||||||||||||||||||||||||||||||
| 470 | 1 | $self->$_([ $uri->$_ ]) for qw( path_segments query_form ); | |||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||
| 473 | 2 | return $old | |||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||
| 476 | =head2 query_param | ||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||
| 478 | @keys = $uri->query_param | ||||||||||||||||||||||||||||||||
| 479 | @values = $uri->query_param($key) | ||||||||||||||||||||||||||||||||
| 480 | @old_values = $uri->query_param($key, @new_values); | ||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||
| 482 | This works exactly like the method of the same name implemented in | ||||||||||||||||||||||||||||||||
| 483 | L<URI::QueryParam>. | ||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||
| 485 | With no arguments, all unique query field names are returned | ||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||
| 487 | With one argument, all values for the given field name are returned | ||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||
| 489 | With more than one argument, values for the given key (first argument) are set | ||||||||||||||||||||||||||||||||
| 490 | to the given values (remaining arguments). Care is taken in this case to | ||||||||||||||||||||||||||||||||
| 491 | preserve the ordering of the fields. | ||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||
| 493 | =cut | ||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||
| 495 | 18 | ▶ | sub query_param { | ||||||||||||||||||||||||||||||
|
Subroutine
query_param: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 496 | 18 | my ($self, $key, @values) = @_; | |||||||||||||||||||||||||||||||
| 497 | 18 | my @form = $self->query_form; | |||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||
| 499 | 18 | ▶ | if ($key) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 500 | 12 | ▶ | my @indices = grep $_ % 2 == 0 && $form[$_] eq $key, 0 .. $#form; | ||||||||||||||||||||||||||||||
|
Condition100%3/3
$_ % 2 == 0 && $form[$_] eq $key
MC/DC100%2/2
$_ % 2 == 0 && $form[$_] eq $key
$_ % 2 == 0
$form[$_] eq $key
Truth table100%3/3
A && B
A= $_ % 2 == 0B= $form[$_] eq $key
| |||||||||||||||||||||||||||||||||
| 501 | 12 | my @old_values = @form[ map $_ + 1, @indices ]; | |||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||
| 503 | 12 | ▶ | if (@values) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 504 | 2 | @values = _flatten @values; | |||||||||||||||||||||||||||||||
| 505 | 2 | ▶ | splice @form, pop @indices, 2 while @indices > @values; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||
| 507 | 2 | ▶ | my $last_index = @indices ? $indices[-1] + 2 : @form; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||
| 509 | 2 | ▶ | while (@values && @indices) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@values and @indices
MC/DC100%2/2
@values and @indices
@values
@indices
Truth table100%3/3
A and B
A= @valuesB= @indices
| |||||||||||||||||||||||||||||||||
| 510 | 3 | splice @form, shift @indices, 2, $key, shift @values; | |||||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||
| 513 | 2 | ▶ | if (@values) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 514 | 1 | splice @form, $last_index, 0, map { $key => $_ } @values; | |||||||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||
| 517 | 2 | $self->query_form(@form); | |||||||||||||||||||||||||||||||
| 518 | } | ||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||
| 520 | 12 | ▶ | return wantarray ? @old_values : $old_values[0]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 521 | } | ||||||||||||||||||||||||||||||||
| 522 | else { | ||||||||||||||||||||||||||||||||
| 523 | 6 | my %seen; | |||||||||||||||||||||||||||||||
| 524 | 6 | return grep !$seen{$_}++, map $form[$_], grep $_ % 2 == 0, 0 .. $#form; | |||||||||||||||||||||||||||||||
| 525 | } | ||||||||||||||||||||||||||||||||
| 526 | } | ||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||
| 528 | =head2 query_param_append | ||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||
| 530 | $uri->query_param_append($key, @values) | ||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||
| 532 | Appends fields to the end of the C<query_form>. Returns nothing. | ||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||
| 534 | =cut | ||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||
| 536 | 1 | ▶ | sub query_param_append { | ||||||||||||||||||||||||||||||
|
Subroutine
query_param_append: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 537 | 1 | my ($self, $key, @values) = @_; | |||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||
| 539 | 1 | $self->query_form($self->query_form, map { $key => $_ } _flatten @values); | |||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||
| 541 | 1 | return; | |||||||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||
| 544 | =head2 query_param_delete | ||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||
| 546 | @old_values = $uri->query_param_delete($key) | ||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||
| 548 | Removes all fields with the given key from the C<query_form>. | ||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||
| 550 | =cut | ||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||
| 552 | 1 | ▶ | sub query_param_delete { | ||||||||||||||||||||||||||||||
|
Subroutine
query_param_delete: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 553 | 1 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||
| 555 | 1 | return $self->query_param($key, []); | |||||||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||
| 558 | =head2 query_form_hash | ||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||
| 560 | $hashref = $uri->query_form_hash | ||||||||||||||||||||||||||||||||
| 561 | $old_hashref = $uri->query_form_hash(\%new_hashref) | ||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||
| 563 | A hash representation of the C<query_form>, with multiple values represented | ||||||||||||||||||||||||||||||||
| 564 | as arrayrefs. | ||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||
| 566 | =cut | ||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||
| 568 | 8 | ▶ | sub query_form_hash { | ||||||||||||||||||||||||||||||
|
Subroutine
query_form_hash: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 569 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||
| 570 | 8 | my @new; | |||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||
| 572 | 8 | ▶ | if (my %form = @_ == 1 && ref $_[0] eq 'HASH' ? %{ $_[0] } : @_) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
@_ == 1 and ref $_[0] eq "HASH"
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1
ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||||||
| 573 | 4 | @new = _flatten_hash(\%form); | |||||||||||||||||||||||||||||||
| 574 | } | ||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||
| 576 | 8 | ▶ | unless (defined wantarray) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 577 | # void context, don't bother building the hash | ||||||||||||||||||||||||||||||||
| 578 | 3 | $self->query_form(@new); | |||||||||||||||||||||||||||||||
| 579 | 3 | return; | |||||||||||||||||||||||||||||||
| 580 | } | ||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||
| 582 | my %form = map { | ||||||||||||||||||||||||||||||||
| 583 | 5 | my @values = $self->query_param($_); | |||||||||||||||||||||||||||||||
| 584 | 7 | ▶ | ( $_ => @values == 1 ? $values[0] : \@values ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 585 | } $self->query_param; | ||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||
| 587 | 5 | ▶ | $self->query_form(@new) if @new; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||
| 589 | 5 | return \%form; | |||||||||||||||||||||||||||||||
| 590 | } | ||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||
| 592 | =head1 TODO | ||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||
| 594 | The following URI methods are currently not implemented: | ||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||
| 596 | =over | ||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||
| 598 | =item * as_iri | ||||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||
| 600 | =item * ihost | ||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||
| 602 | =back | ||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||
| 604 | =head1 LICENSE | ||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||
| 606 | L<perlartistic> | ||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||
| 608 | =cut | ||||||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||||||
| 610 | 4 | 1; | |||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||
| 612 | __END__ | ||||||||||||||||||||||||||||||||