/usr/share/perl5/Mojo/Parameters.pm is in libmojolicious-perl 2.98+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | package Mojo::Parameters;
use Mojo::Base -base;
use overload
'bool' => sub {1},
'""' => sub { shift->to_string },
fallback => 1;
use Mojo::Util qw(decode encode url_escape url_unescape);
use Mojo::URL;
has charset => 'UTF-8';
has pair_separator => '&';
# "Yeah, Moe, that team sure did suck last night. They just plain sucked!
# I've seen teams suck before,
# but they were the suckiest bunch of sucks that ever sucked!
# HOMER!
# I gotta go Moe my damn weiner kids are listening."
sub new {
my $self = shift->SUPER::new;
# Pairs
if (@_ > 1) { $self->append(@_) }
# String
else { $self->{string} = $_[0] }
return $self;
}
sub append {
my ($self, @pairs) = @_;
my $params = $self->params;
for (my $i = 0; $i < @pairs; $i += 2) {
my $key = $pairs[$i] // '';
my $value = $pairs[$i + 1] // '';
# Single value
if (ref $value ne 'ARRAY') { push @$params, $key => $value }
# Multiple values
else { push @$params, $key => (defined $_ ? "$_" : '') for @$value }
}
return $self;
}
sub clone {
my $self = shift;
my $clone = Mojo::Parameters->new;
$clone->pair_separator($self->pair_separator);
if (defined $self->{string}) { $clone->{string} = $self->{string} }
else { $clone->params([@{$self->params}]) }
return $clone;
}
sub merge {
my $self = shift;
push @{$self->params}, @{$_->params} for @_;
return $self;
}
sub param {
my ($self, $name) = (shift, shift);
# List names
return sort keys %{$self->to_hash} unless $name;
# Replace values
$self->remove($name) if defined $_[0];
$self->append($name, $_) for @_;
# List values
my @values;
my $params = $self->params;
for (my $i = 0; $i < @$params; $i += 2) {
push @values, $params->[$i + 1] if $params->[$i] eq $name;
}
return wantarray ? @values : $values[0];
}
sub params {
my ($self, $params) = @_;
if ($params) { $self->{params} = $params and return $self }
elsif (defined $self->{string}) { $self->parse }
return $self->{params} ||= [];
}
sub parse {
my ($self, $string) = @_;
$string //= $self->{string};
# Clear
delete $self->{string};
$self->params([]);
# Detect pair separator for reconstruction
return $self unless defined $string && length $string;
$self->pair_separator(';') if $string =~ /;/ && $string !~ /\&/;
# W3C suggests to also accept ";" as a separator
my $charset = $self->charset;
for my $pair (split /[\&\;]+/, $string) {
# Parse
$pair =~ /^([^=]*)(?:=(.*))?$/;
my $name = $1 // '';
my $value = $2 // '';
# Replace "+" with whitespace
$name =~ s/\+/\ /g;
$value =~ s/\+/\ /g;
# Unescape
if (index($name, '%') >= 0) {
$name = url_unescape $name;
$name = decode($charset, $name) // $name if $charset;
}
if (index($value, '%') >= 0) {
$value = url_unescape $value;
$value = decode($charset, $value) // $value if $charset;
}
push @{$self->params}, $name, $value;
}
return $self;
}
# "Don't kid yourself, Jimmy. If a cow ever got the chance,
# he'd eat you and everyone you care about!"
sub remove {
my ($self, $name) = @_;
$name //= '';
# Remove
my $params = $self->params;
for (my $i = 0; $i < @$params;) {
if ($params->[$i] eq $name) { splice @$params, $i, 2 }
else { $i += 2 }
}
return $self->params($params);
}
sub to_hash {
my $self = shift;
# Format
my $params = $self->params;
my %params;
for (my $i = 0; $i < @$params; $i += 2) {
my $name = $params->[$i];
my $value = $params->[$i + 1];
# Array
if (exists $params{$name}) {
$params{$name} = [$params{$name}] unless ref $params{$name} eq 'ARRAY';
push @{$params{$name}}, $value;
}
# String
else { $params{$name} = $value }
}
return \%params;
}
sub to_string {
my $self = shift;
# String
my $charset = $self->charset;
if (defined(my $string = $self->{string})) {
$string = encode $charset, $string if $charset;
return url_escape $string, "^$Mojo::URL::UNRESERVED&;=+%";
}
# Build pairs
my $params = $self->params;
return '' unless @{$self->params};
my @params;
for (my $i = 0; $i < @$params; $i += 2) {
my $name = $params->[$i];
my $value = $params->[$i + 1];
# Escape
$name = encode $charset, $name if $charset;
$name = url_escape $name, "^$Mojo::URL::UNRESERVED";
if ($value) {
$value = encode $charset, $value if $charset;
$value = url_escape $value, "^$Mojo::URL::UNRESERVED";
}
# Replace whitespace with "+"
$name =~ s/\%20/\+/g;
$value =~ s/\%20/\+/g if $value;
push @params, defined $value ? "$name=$value" : "$name";
}
# Concatenate pairs
my $separator = $self->pair_separator;
return join $separator, @params;
}
1;
=head1 NAME
Mojo::Parameters - Parameter container
=head1 SYNOPSIS
use Mojo::Parameters;
my $p = Mojo::Parameters->new(foo => 'bar', baz => 23);
=head1 DESCRIPTION
L<Mojo::Parameters> is a container for form parameters.
=head1 ATTRIBUTES
L<Mojo::Parameters> implements the following attributes.
=head2 C<charset>
my $charset = $p->charset;
$p = $p->charset('UTF-8');
Charset used for decoding parameters, defaults to C<UTF-8>.
=head2 C<pair_separator>
my $separator = $p->pair_separator;
$p = $p->pair_separator(';');
Separator for parameter pairs, defaults to C<&>.
=head1 METHODS
L<Mojo::Parameters> inherits all methods from L<Mojo::Base> and implements the
following new ones.
=head2 C<new>
my $p = Mojo::Parameters->new;
my $p = Mojo::Parameters->new('foo=b%3Bar&baz=23');
my $p = Mojo::Parameters->new(foo => 'b;ar');
my $p = Mojo::Parameters->new(foo => ['ba;r', 'b;az']);
my $p = Mojo::Parameters->new(foo => ['ba;r', 'b;az'], bar => 23);
Construct a new L<Mojo::Parameters> object.
=head2 C<append>
$p = $p->append(foo => 'ba;r');
$p = $p->append(foo => ['ba;r', 'b;az']);
$p = $p->append(foo => ['ba;r', 'b;az'], bar => 23);
Append parameters.
# "foo=bar&foo=baz"
Mojo::Parameters->new('foo=bar')->append(foo => 'baz');
# "foo=bar&foo=baz&foo=yada"
Mojo::Parameters->new('foo=bar')->append(foo => ['baz', 'yada']);
# "foo=bar&foo=baz&foo=yada&bar=23"
Mojo::Parameters->new('foo=bar')->append(foo => ['baz', 'yada'], bar => 23);
=head2 C<clone>
my $p2 = $p->clone;
Clone parameters.
=head2 C<merge>
$p = $p->merge(Mojo::Parameters->new(foo => 'b;ar', baz => 23));
Merge parameters.
=head2 C<param>
my @names = $p->param;
my $foo = $p->param('foo');
my @foo = $p->param('foo');
my $foo = $p->param(foo => 'ba;r');
my @foo = $p->param(foo => qw(ba;r ba;z));
Check and replace parameter values.
=head2 C<params>
my $params = $p->params;
$p = $p->params([foo => 'b;ar', baz => 23]);
Parsed parameters.
=head2 C<parse>
$p = $p->parse('foo=b%3Bar&baz=23');
Parse parameters.
=head2 C<remove>
$p = $p->remove('foo');
Remove parameters.
# "bar=yada"
Mojo::Parameters->new('foo=bar&foo=baz&bar=yada')->remove('foo');
=head2 C<to_hash>
my $hash = $p->to_hash;
Turn parameters into a hash reference.
# "baz"
Mojo::Parameters->new('foo=bar&foo=baz')->to_hash->{foo}[1];
=head2 C<to_string>
my $string = $p->to_string;
Turn parameters into a string.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut
|