1Mojo::Parameters(3) User Contributed Perl Documentation Mojo::Parameters(3)
2
3
4
6 Mojo::Parameters - Parameters
7
9 use Mojo::Parameters;
10
11 # Parse
12 my $params = Mojo::Parameters->new('foo=bar&baz=23');
13 say $params->param('baz');
14
15 # Build
16 my $params = Mojo::Parameters->new(foo => 'bar', baz => 23);
17 push @$params, i => '♥ mojolicious';
18 say "$params";
19
21 Mojo::Parameters is a container for form parameters used by Mojo::URL,
22 based on RFC 3986 <https://tools.ietf.org/html/rfc3986> and the HTML
23 Living Standard <https://html.spec.whatwg.org>.
24
26 Mojo::Parameters implements the following attributes.
27
28 charset
29 my $charset = $params->charset;
30 $params = $params->charset('UTF-8');
31
32 Charset used for encoding and decoding parameters, defaults to "UTF-8".
33
34 # Disable encoding and decoding
35 $params->charset(undef);
36
38 Mojo::Parameters inherits all methods from Mojo::Base and implements
39 the following new ones.
40
41 append
42 $params = $params->append(foo => 'ba&r');
43 $params = $params->append(foo => ['ba&r', 'baz']);
44 $params = $params->append(foo => ['bar', 'baz'], bar => 23);
45 $params = $params->append(Mojo::Parameters->new);
46
47 Append parameters. Note that this method will normalize the parameters.
48
49 # "foo=bar&foo=baz"
50 Mojo::Parameters->new('foo=bar')->append(Mojo::Parameters->new('foo=baz'));
51
52 # "foo=bar&foo=baz"
53 Mojo::Parameters->new('foo=bar')->append(foo => 'baz');
54
55 # "foo=bar&foo=baz&foo=yada"
56 Mojo::Parameters->new('foo=bar')->append(foo => ['baz', 'yada']);
57
58 # "foo=bar&foo=baz&foo=yada&bar=23"
59 Mojo::Parameters->new('foo=bar')->append(foo => ['baz', 'yada'], bar => 23);
60
61 clone
62 my $params2 = $params->clone;
63
64 Return a new Mojo::Parameters object cloned from these parameters.
65
66 every_param
67 my $values = $params->every_param('foo');
68
69 Similar to "param", but returns all values sharing the same name as an
70 array reference. Note that this method will normalize the parameters.
71
72 # Get first value
73 say $params->every_param('foo')->[0];
74
75 merge
76 $params = $params->merge(foo => 'ba&r');
77 $params = $params->merge(foo => ['ba&r', 'baz']);
78 $params = $params->merge(foo => ['bar', 'baz'], bar => 23);
79 $params = $params->merge(Mojo::Parameters->new);
80
81 Merge parameters. Note that this method will normalize the parameters.
82
83 # "foo=baz"
84 Mojo::Parameters->new('foo=bar')->merge(Mojo::Parameters->new('foo=baz'));
85
86 # "yada=yada&foo=baz"
87 Mojo::Parameters->new('foo=bar&yada=yada')->merge(foo => 'baz');
88
89 # "yada=yada"
90 Mojo::Parameters->new('foo=bar&yada=yada')->merge(foo => undef);
91
92 names
93 my $names = $params->names;
94
95 Return an array reference with all parameter names.
96
97 # Names of all parameters
98 say for @{$params->names};
99
100 new
101 my $params = Mojo::Parameters->new;
102 my $params = Mojo::Parameters->new('foo=b%3Bar&baz=23');
103 my $params = Mojo::Parameters->new(foo => 'b&ar');
104 my $params = Mojo::Parameters->new(foo => ['ba&r', 'baz']);
105 my $params = Mojo::Parameters->new(foo => ['bar', 'baz'], bar => 23);
106
107 Construct a new Mojo::Parameters object and "parse" parameters if
108 necessary.
109
110 pairs
111 my $array = $params->pairs;
112 $params = $params->pairs([foo => 'b&ar', baz => 23]);
113
114 Parsed parameter pairs. Note that this method will normalize the
115 parameters.
116
117 # Remove all parameters
118 $params->pairs([]);
119
120 param
121 my $value = $params->param('foo');
122 $params = $params->param(foo => 'ba&r');
123 $params = $params->param(foo => qw(ba&r baz));
124 $params = $params->param(foo => ['ba;r', 'baz']);
125
126 Access parameter values. If there are multiple values sharing the same
127 name, and you want to access more than just the last one, you can use
128 "every_param". Note that this method will normalize the parameters.
129
130 parse
131 $params = $params->parse('foo=b%3Bar&baz=23');
132
133 Parse parameters.
134
135 remove
136 $params = $params->remove('foo');
137
138 Remove parameters. Note that this method will normalize the parameters.
139
140 # "bar=yada"
141 Mojo::Parameters->new('foo=bar&foo=baz&bar=yada')->remove('foo');
142
143 to_hash
144 my $hash = $params->to_hash;
145
146 Turn parameters into a hash reference. Note that this method will
147 normalize the parameters.
148
149 # "baz"
150 Mojo::Parameters->new('foo=bar&foo=baz')->to_hash->{foo}[1];
151
152 to_string
153 my $str = $params->to_string;
154
155 Turn parameters into a string.
156
157 # "foo=bar&baz=23"
158 Mojo::Parameters->new->pairs([foo => 'bar', baz => 23])->to_string;
159
161 Mojo::Parameters overloads the following operators.
162
163 array
164 my @pairs = @$params;
165
166 Alias for "pairs". Note that this will normalize the parameters.
167
168 say $params->[0];
169 say for @$params;
170
171 bool
172 my $bool = !!$params;
173
174 Always true.
175
176 stringify
177 my $str = "$params";
178
179 Alias for "to_string".
180
182 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
183
184
185
186perl v5.34.0 2021-07-22 Mojo::Parameters(3)