DOKK / manpages / debian 11 / libmojolicious-perl / Mojo::Parameters.3pm.en
Mojo::Parameters(3pm) User Contributed Perl Documentation Mojo::Parameters(3pm)

Mojo::Parameters - Parameters

  use Mojo::Parameters;
  # Parse
  my $params = Mojo::Parameters->new('foo=bar&baz=23');
  say $params->param('baz');
  # Build
  my $params = Mojo::Parameters->new(foo => 'bar', baz => 23);
  push @$params, i => '♥ mojolicious';
  say "$params";

Mojo::Parameters is a container for form parameters used by Mojo::URL, based on RFC 3986 <https://tools.ietf.org/html/rfc3986> and the HTML Living Standard <https://html.spec.whatwg.org>.

Mojo::Parameters implements the following attributes.

  my $charset = $params->charset;
  $params     = $params->charset('UTF-8');

Charset used for encoding and decoding parameters, defaults to "UTF-8".

  # Disable encoding and decoding
  $params->charset(undef);

Mojo::Parameters inherits all methods from Mojo::Base and implements the following new ones.

  $params = $params->append(foo => 'ba&r');
  $params = $params->append(foo => ['ba&r', 'baz']);
  $params = $params->append(foo => ['bar', 'baz'], bar => 23);
  $params = $params->append(Mojo::Parameters->new);

Append parameters. Note that this method will normalize the parameters.

  # "foo=bar&foo=baz"
  Mojo::Parameters->new('foo=bar')->append(Mojo::Parameters->new('foo=baz'));
  # "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);

  my $params2 = $params->clone;

Return a new Mojo::Parameters object cloned from these parameters.

  my $values = $params->every_param('foo');

Similar to "param", but returns all values sharing the same name as an array reference. Note that this method will normalize the parameters.

  # Get first value
  say $params->every_param('foo')->[0];

  $params = $params->merge(foo => 'ba&r');
  $params = $params->merge(foo => ['ba&r', 'baz']);
  $params = $params->merge(foo => ['bar', 'baz'], bar => 23);
  $params = $params->merge(Mojo::Parameters->new);

Merge parameters. Note that this method will normalize the parameters.

  # "foo=baz"
  Mojo::Parameters->new('foo=bar')->merge(Mojo::Parameters->new('foo=baz'));
  # "yada=yada&foo=baz"
  Mojo::Parameters->new('foo=bar&yada=yada')->merge(foo => 'baz');
  # "yada=yada"
  Mojo::Parameters->new('foo=bar&yada=yada')->merge(foo => undef);

  my $names = $params->names;

Return an array reference with all parameter names.

  # Names of all parameters
  say for @{$params->names};

  my $params = Mojo::Parameters->new;
  my $params = Mojo::Parameters->new('foo=b%3Bar&baz=23');
  my $params = Mojo::Parameters->new(foo => 'b&ar');
  my $params = Mojo::Parameters->new(foo => ['ba&r', 'baz']);
  my $params = Mojo::Parameters->new(foo => ['bar', 'baz'], bar => 23);

Construct a new Mojo::Parameters object and "parse" parameters if necessary.

  my $array = $params->pairs;
  $params   = $params->pairs([foo => 'b&ar', baz => 23]);

Parsed parameter pairs. Note that this method will normalize the parameters.

  # Remove all parameters
  $params->pairs([]);

  my $value = $params->param('foo');
  $params   = $params->param(foo => 'ba&r');
  $params   = $params->param(foo => qw(ba&r baz));
  $params   = $params->param(foo => ['ba;r', 'baz']);

Access parameter values. If there are multiple values sharing the same name, and you want to access more than just the last one, you can use "every_param". Note that this method will normalize the parameters.

  $params = $params->parse('foo=b%3Bar&baz=23');

Parse parameters.

  $params = $params->remove('foo');

Remove parameters. Note that this method will normalize the parameters.

  # "bar=yada"
  Mojo::Parameters->new('foo=bar&foo=baz&bar=yada')->remove('foo');

  my $hash = $params->to_hash;

Turn parameters into a hash reference. Note that this method will normalize the parameters.

  # "baz"
  Mojo::Parameters->new('foo=bar&foo=baz')->to_hash->{foo}[1];

  my $str = $params->to_string;

Turn parameters into a string.

  # "foo=bar&baz=23"
  Mojo::Parameters->new->pairs([foo => 'bar', baz => 23])->to_string;

Mojo::Parameters overloads the following operators.

  my @pairs = @$params;

Alias for "pairs". Note that this will normalize the parameters.

  say $params->[0];
  say for @$params;

  my $bool = !!$params;

Always true.

  my $str = "$params";

Alias for "to_string".

Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.

2021-01-24 perl v5.32.0