| 1 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # Class providing URI query string manipulation | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | package URI::Query; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use 5.00503; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use parent 'Clone'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use URI::Escape qw(uri_escape_utf8 uri_unescape); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 9 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | use overload | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 9 | ▶ | '""' => \&stringify, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | 'eq' => sub { $_[0]->stringify eq $_[1]->stringify }, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 9 | ▶ | 'ne' => sub { $_[0]->stringify ne $_[1]->stringify }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 9 | ▶ | use vars q($VERSION); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 9 | $VERSION = '0.16'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # Remove all occurrences of the given parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | sub strip | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 7 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
strip: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 7 | foreach (@_) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 10 | ▶ | delete $self->{qq}->{$_} and $self->{changed}++; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 7 | $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # Remove all parameters except those given | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | sub strip_except | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
strip_except: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | my %keep = map { $_ => 1 } @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 3 | foreach (keys %{$self->{qq}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 13 | ▶ | next if $keep{$_}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 5 | ▶ | delete $self->{qq}->{$_} and $self->{changed}++; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # Remove all empty/undefined parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | sub strip_null | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
strip_null: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 2 | foreach (keys %{$self->{qq}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 9 | ▶ | next if @{$self->{qq}->{$_}}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 3 | ▶ | delete $self->{qq}->{$_} and $self->{changed}++; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 2 | $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # Remove all parameters matching $re | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | sub strip_like | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
strip_like: called CC=8 SCAR=22.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 3 | ▶ | my $re = shift or croak "Missing regex param to strip_like"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 3 | ▶ | croak "Invalid param '$re' to strip_like - must be regex" if ! ref $re || ref $re ne 'Regexp'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not ref $re or ref $re ne "Regexp"
MC/DC0%0/2
not ref $re or ref $re ne "Regexp"
not ref $re
ref $re ne "Regexp"
Truth table33%1/3
not(A) or B
A= not ref $reB= ref $re ne "Regexp"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 3 | ▶ | croak "Too many params to strip_like - only one permitted" if @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 3 | foreach (keys %{$self->{qq}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 10 | ▶ | next if $_ !~ $re; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 3 | ▶ | delete $self->{qq}->{$_} and $self->{changed}++; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 3 | $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # Replace all occurrences of the given parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | sub replace | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replace: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | my %arg = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | for my $key (keys %arg) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 5 | $self->{qq}->{$key} = []; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 5 | ▶ | if (ref $arg{$key} eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | push @{$self->{qq}->{$key}}, $_ foreach @{$arg{$key}}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 4 | push @{$self->{qq}->{$key}}, $arg{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 5 | $self->{changed}++; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # Return the stringified qq hash | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | sub stringify | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 91 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stringify: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 91 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 91 | ▶ | my $sep = shift || $self->{sep} || '&'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%5/5
shift() || $self->{'sep'} || '&'
shift() or $self->{"sep"}
MC/DC100%2/2
shift() || $self->{'sep'} || '&'
shift()
$self->{"sep"}
Truth table100%3/3
A or B
A= shift()B= $self->{"sep"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 91 | my @out = (); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 91 | for my $key (sort keys %{$self->{qq}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 262 | for my $value (@{$self->{qq}->{$key}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 382 | push @out, sprintf("%s=%s", uri_escape_utf8($key), uri_escape_utf8($value)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 91 | join $sep, @out; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | # Return the stringified qq hash with a leading '?' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | sub qstringify | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
qstringify: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 3 | return '?' . $self->stringify(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | sub revert | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 4 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
revert: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | # Revert qq to the qq_orig hashref | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 4 | $self->{qq} = $self->_deepcopy($self->{qq_orig}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 4 | $self->{changed} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 4 | $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 9 | ▶ | sub has_changed { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_changed: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 9 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 9 | ▶ | $self->{changed} > 0 ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | # ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | # Convenience methods | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | # Return the current qq hash(ref) with one-elt arrays flattened | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | sub hash | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 4 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hash: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 4 | my %qq = %{$self->{qq}}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | # Flatten one element arrays | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 4 | for (sort keys %qq) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 10 | ▶ | $qq{$_} = $qq{$_}->[0] if @{$qq{$_}} == 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 4 | ▶ | return wantarray ? %qq : \%qq; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | # Return the current qq hash(ref) with all elements as arrayrefs | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | sub hash_arrayref | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hash_arrayref: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | my %qq = %{$self->{qq}}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | # (Don't flatten one element arrays) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | ▶ | return wantarray ? %qq : \%qq; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | # Return the current query as a string of html hidden input tags | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | sub hidden | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hidden: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 1 | my $str = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 1 | for my $key (sort keys %{$self->{qq}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 4 | for my $value (@{$self->{qq}->{$key}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 6 | $str .= qq(<input type="hidden" name="$key" value="$value" />\n); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 1 | return $str; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | # ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # Set the output separator to use by default | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | sub separator | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
separator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 1 | $self->{sep} = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | # Deep copy routine, originally swiped from a Randal Schwartz column | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | sub _deepcopy | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 231 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_deepcopy: called CC=6 SCAR=21.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 231 | my ($self, $this) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 231 | ▶ | if (! ref $this) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 116 | return $this; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } elsif (ref $this eq "ARRAY") { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 85 | return [map $self->_deepcopy($_), @$this]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | } elsif (ref $this eq "HASH") { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 30 | return {map { $_ => $self->_deepcopy($this->{$_}) } keys %$this}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | } elsif (ref $this eq "CODE") { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | return $this; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | } elsif (sprintf $this) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | # Object! As a last resort, try copying the stringification value | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | return sprintf $this; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | die "what type is $_? (" . ref($this) . ")"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | # Parse query string, storing as hash (qq) of key => arrayref pairs | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | sub _parse_qs | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 15 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_qs: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 15 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 15 | my $qs = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 15 | for (split /[&;]/, $qs) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 70 | my ($key, $value) = map { uri_unescape($_) } split /=/, $_, 2; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 70 | ▶ | $self->{qq}->{$key} ||= []; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"qq"}{$key} ||= []
MC/DC100%1/1
$self->{"qq"}{$key} ||= []
$self->{"qq"}{$key}
Truth table100%2/2
A
A= $self->{"qq"}{$key}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 70 | ▶ | push @{$self->{qq}->{$key}}, $value if defined $value && $value ne ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $value and $value ne ""
MC/DC50%1/2
defined $value and $value ne ""
defined $value
$value ne ""
Truth table66%2/3
A and B
A= defined $valueB= $value ne ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | $self | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 15 | } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | # Process arrayref arguments into hash (qq) of key => arrayref pairs | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | sub _init_from_arrayref | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 6 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init_from_arrayref: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 6 | my ($self, $arrayref) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 6 | ▶ | while (@$arrayref) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 24 | my $key = shift @$arrayref; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 24 | my $value = shift @$arrayref; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 24 | my $key_unesc = uri_unescape($key); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 24 | ▶ | $self->{qq}->{$key_unesc} ||= []; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"qq"}{$key_unesc} ||= []
MC/DC100%1/1
$self->{"qq"}{$key_unesc} ||= []
$self->{"qq"}{$key_unesc}
Truth table100%2/2
A
A= $self->{"qq"}{$key_unesc}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 24 | ▶ | if (defined $value && $value ne '') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and $value ne ""
MC/DC100%2/2
defined $value and $value ne ""
defined $value
$value ne ""
Truth table100%3/3
A and B
A= defined $valueB= $value ne ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 22 | my @values; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 22 | ▶ | if (! ref $value) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 20 | @values = split "\0", $value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | elsif (ref $value eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 2 | @values = @$value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | die "Invalid value found: $value. Not string or arrayref!"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 22 | push @{$self->{qq}->{$key_unesc}}, map { uri_unescape($_) } @values; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | # Constructor - either new($qs) where $qs is a scalar query string or a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | # a hashref of key => value pairs, or new(key => val, key => val); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | # In the array form, keys can repeat, and/or values can be arrayrefs. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | sub new | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 26 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 26 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 26 | my $self = bless { qq => {} }, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 26 | ▶ | if (@_ == 1 && ! ref $_[0] && $_[0]) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%6/6
@_ == 1 and not ref $_[0] and $_[0]
@_ == 1 and not ref $_[0]
MC/DC100%3/3
@_ == 1 and not ref $_[0] and $_[0]
@_ == 1
not ref $_[0]
$_[0]
Truth table100%4/4
A and not(B) and C
A= @_ == 1B= not ref $_[0]C= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 15 | $self->_parse_qs($_[0]); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | ▶ | elsif (@_ == 1 && ref $_[0] eq 'HASH') { | |||||||||||||||||||||||||||||||||||||||||||||||||
|
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 3 | $self->_init_from_arrayref([ %{$_[0]} ]); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | elsif (scalar(@_) % 2 == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 3 | $self->_init_from_arrayref(\@_); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | # Clone the qq hashref to allow reversion | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 26 | $self->{qq_orig} = $self->_deepcopy($self->{qq}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | # Changed flag | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 26 | $self->{changed} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 26 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | # ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | URI::Query - class providing URI query string manipulation | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | # Constructor - using a GET query string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | $qq = URI::Query->new($query_string); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | # OR Constructor - using a hashref of key => value parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | $qq = URI::Query->new($cgi->Vars); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | # OR Constructor - using an array of successive keys and values | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | $qq = URI::Query->new(@params); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | # Clone the current object | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | $qq2 = $qq->clone; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | # Revert back to the initial constructor state (to do it all again) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | $qq->revert; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | # Remove all occurrences of the given parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | $qq->strip('page', 'next'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | # Remove all parameters except the given ones | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | $qq->strip_except('pagesize', 'order'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | # Remove all empty/undefined parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | $qq->strip_null; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | # Replace all occurrences of the given parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | $qq->replace(page => $page, foo => 'bar'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | # Set the argument separator to use for output (default: unescaped '&') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | $qq->separator(';'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | # Output the current query string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | print "$qq"; # OR $qq->stringify; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | # Stringify with explicit argument separator | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | $qq->stringify(';'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | # Output the current query string with a leading '?' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | $qq->qstringify; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # Stringify with a leading '?' and an explicit argument separator | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | $qq->qstringify(';'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | # Get a flattened hash/hashref of the current parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | # (single item parameters as scalars, multiples as an arrayref) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | my %qq = $qq->hash; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | # Get a non-flattened hash/hashref of the current parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | # (parameter => arrayref of values) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | my %qq = $qq->hash_arrayref; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | # Get the current query string as a set of hidden input tags | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | print $qq->hidden; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | # Check whether the query has changed since construction | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | if ($qq->has_changed) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | print "changed version: $qq\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | URI::Query provides simple URI query string manipulation, allowing you | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | to create and manipulate URI query strings from GET and POST requests in | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | web applications. This is primarily useful for creating links where you | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | wish to preserve some subset of the parameters to the current request, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | and potentially add or replace others. Given a query string this is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | doable with regexes, of course, but making sure you get the anchoring | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | and escaping right is tedious and error-prone - this module is simpler. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | =head2 CONSTRUCTOR | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | URI::Query objects can be constructed from scalar query strings | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | ('foo=1&bar=2&bar=3'), from a hashref which has parameters as keys, and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | values either as scalars or arrayrefs of scalars (to handle the case of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | parameters with multiple values e.g. { foo => '1', bar => [ '2', '3' ] }), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | or arrays composed of successive parameters-value pairs | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | e.g. ('foo', '1', 'bar', '2', 'bar', '3'). For instance: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | # Constructor - using a GET query string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | $qq = URI::Query->new($query_string); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | # Constructor - using an array of successive keys and values | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | $qq = URI::Query->new(@params); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | # Constructor - using a hashref of key => value parameters, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | # where values are either scalars or arrayrefs of scalars | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | $qq = URI::Query->new($cgi->Vars); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | URI::Query also handles L<CGI.pm>-style hashrefs, where multiple | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | values are packed into a single string, separated by the "\0" (null) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | character. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | All keys and values are URI unescaped at construction time, and are | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | stored and referenced unescaped. So a query string like: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | group=prod%2Cinfra%2Ctest&op%3Aset=x%3Dy | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | is stored as: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 'group' => 'prod,infra,test' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 'op:set' => 'x=y' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | You should always use the unescaped/normal variants in methods i.e. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | $qq->replace('op:set' => 'x=z'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | NOT: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | $qq->replace('op%3Aset' => 'x%3Dz'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | You can also construct a new URI::Query object by cloning an existing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | one: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | $qq2 = $qq->clone; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | =head2 MODIFIER METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | All modifier methods change the state of the URI::Query object in some | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | way, and return $self, so they can be used in chained style e.g. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | $qq->revert->strip('foo')->replace(bar => 123); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | Note that URI::Query stashes a copy of the parameter set that existed | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | at construction time, so that any changes made by these methods can be | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | rolled back using 'revert()'. So you don't (usually) need to keep | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | multiple copies around to handle incompatible changes. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | =item revert() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | Revert the current parameter set back to that originally given at | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | construction time i.e. discard all changes made since construction. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | =item strip($param1, $param2, ...) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | Remove all occurrences of the given parameters and their values from | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | the current parameter set. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | =item strip_except($param1, $param2, ...) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | Remove all parameters EXCEPT those given from the current parameter | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | set. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | =item strip_null() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | Remove all parameters that have a value of undef from the current | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | parameter set. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | =item replace($param1 => $value1, $param2, $value2, ...) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | Replace the values of the given parameters in the current parameter set | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | with these new ones. Parameter names must be scalars, but values can be | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | either scalars or arrayrefs of scalars, when multiple values are desired. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | Note that 'replace' can also be used to add or append, since there's | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | no requirement that the parameters already exist in the current parameter | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | set. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | =item strip_like($regex) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | Remove all parameters whose names match the given (qr-quoted) regex e.g. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | $qq->strip_like(qr/^utm/) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | Does NOT match against parameter values. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | =item separator($separator) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | Set the argument separator to use for output. Default: '&'. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | =head2 ACCESSOR METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | =item has_changed() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | If the query is actually changed by any of the modifier methods (strip, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | strip_except, strip_null, strip_like, or replace) it sets an internal | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | changed flag which can be access by: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | $qq->has_changed | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | revert() resets the has_changed flag to false. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | =head2 OUTPUT METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | =item "$qq", stringify(), stringify($separator) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | Return the current parameter set as a conventional param=value query | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | string, using $separator as the separator if given. e.g. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | foo=1&bar=2&bar=3 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | Note that all parameters and values are URI escaped by stringify(), so | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | that query-string reserved characters do not occur within elements. For | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | instance, a parameter set of: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 'group' => 'prod,infra,test' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 'op:set' => 'x=y' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | will be stringified as: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | group=prod%2Cinfra%2Ctest&op%3Aset=x%3Dy | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | =item qstringify(), qstringify($separator) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | Convenience method to stringify with a leading '?' e.g. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | ?foo=1&bar=2&bar=3 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | =item hash() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | Return a hash (in list context) or hashref (in scalar context) of the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | current parameter set. Single-item parameters have scalar values, while | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | while multiple-item parameters have arrayref values e.g. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | foo => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | bar => [ 2, 3 ], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | =item hash_arrayref() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | Return a hash (in list context) or hashref (in scalar context) of the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | current parameter set. All values are returned as arrayrefs, including | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | those with single values e.g. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | foo => [ 1 ], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | bar => [ 2, 3 ], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | =item hidden() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | Returns the current parameter set as a concatenated string of hidden | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | input tags, one per parameter-value e.g. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | <input type="hidden" name="foo" value="1" /> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | <input type="hidden" name="bar" value="2" /> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | <input type="hidden" name="bar" value="3" /> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | =head1 BUGS AND CAVEATS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | Please report bugs and/or feature requests to | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | C<bug-uri-query at rt.cpan.org>, or through | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | the web interface at | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=URI-Query>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | Should allow unescaping of input to be turned off, for situations in | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | which it's already been done. Please let me know if you find you | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | actually need this. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | I don't think it makes sense on the output side though, since you need | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | to understand the structure of the query to escape elements correctly. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | =head1 PATCHES | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | URI::Query code lives at L<https://github.com/gavincarr/URI-Query>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | Patches / pull requests welcome! | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | Gavin Carr <gavin@openfusion.com.au> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | Copyright 2004-2015, Gavin Carr. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | This program is free software. You may copy or redistribute it under the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | same terms as perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | =for Pod::Coverage new | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | # vim:sw=4:et | ||||||||||||||||||||||||||||||||||||||||||||||||||