Get

parameters --param, every_param

To get the parameters, use the param method and every_param method.

# One parameter
my $name = $c->param('name');

# All parameters
my $names = $c->every_param('name');

In Mojolicious, parameters often include the following three elements, so let's first distinguish between the following three.

  1. Data contained in the query string
  2. Data contained in the HTTP body sent by POST
  3. Data captured in the URL

(1) Data contained in the query string

The query string is the data set after? In the URL.

http://somehost.com/search?name=Ken&age=19

Each parameter "parameter name: value" is connected by "&". Query string parameters are often used when searching for something.

(2) Data contained in the HTTP body sent by POST

This is the data contained in the HTTP body, such as when submitting a form.

Data captured in the URL

Mojolicious allows you to capture a portion of the URL as a parameter.

get'/ entry /: id'=> sub {...};

This value is also treated as a parameter.

Get parameter values ​​

To get the value of the parameter, use the param method of Mojolicious::Controller. This includes all of the above (1), (2), (3).

my $age = $c->param('age');

Get multiple values ​​

Call the every_param method to get multiple selected values ​​such as checkboxes.

my $contries = $c->every_param('country');

Caution : param ($name) in list context has been removed in Mojolicious 6 due to security issues. Be sure to use every_param.

Get the names and values ​​of all parameters

Use "$c->req->params->to_hash" to get all the parameter names.

my $params = $c->req->params->to_hash</pre>

<b> Caution </b>: $c->paramin list context is obsolete from Mojolicious 6 for security reasons. To get all the names, get the keys in "keys @$params" after getting the names and values ​​above.

Below is a sample from Mojolicious::Lite.

<pre>
#Mojolicious::Lite
use Mojolicious::Lite;

use utf8;

get'/ entry /: id'=> sub {
  my $self = shift;
  my $age = $self->param('age');
  my @countries = $self->param('country');
  my @names = $self->param;
};;

Below is a sample from Mojolicious.

package MyApp::Diary;

use Mojo::Base'Mojolicious::Controller';

sub entry {
  my $self = shift;
  my $age = $self->param('age');
  my @countries = $self->param('country');
  my @names = $self->param;
}

Associated Information