1Mojo::Util(3) User Contributed Perl Documentation Mojo::Util(3)
2
3
4
6 Mojo::Util - Portable utility functions
7
9 use Mojo::Util qw(b64_encode url_escape url_unescape);
10
11 my $str = 'test=23';
12 my $escaped = url_escape $str;
13 say url_unescape $escaped;
14 say b64_encode $escaped, '';
15
17 Mojo::Util provides portable utility functions for Mojo.
18
20 Mojo::Util implements the following functions, which can be imported
21 individually.
22
23 b64_decode
24 my $bytes = b64_decode $b64;
25
26 Base64 decode bytes with MIME::Base64.
27
28 b64_encode
29 my $b64 = b64_encode $bytes;
30 my $b64 = b64_encode $bytes, "\n";
31
32 Base64 encode bytes with MIME::Base64, the line ending defaults to a
33 newline.
34
35 camelize
36 my $camelcase = camelize $snakecase;
37
38 Convert "snake_case" string to "CamelCase" and replace "-" with "::".
39
40 # "FooBar"
41 camelize 'foo_bar';
42
43 # "FooBar::Baz"
44 camelize 'foo_bar-baz';
45
46 # "FooBar::Baz"
47 camelize 'FooBar::Baz';
48
49 class_to_file
50 my $file = class_to_file 'Foo::Bar';
51
52 Convert a class name to a file.
53
54 # "foo_bar"
55 class_to_file 'Foo::Bar';
56
57 # "foobar"
58 class_to_file 'FOO::Bar';
59
60 # "foo_bar"
61 class_to_file 'FooBar';
62
63 # "foobar"
64 class_to_file 'FOOBar';
65
66 class_to_path
67 my $path = class_to_path 'Foo::Bar';
68
69 Convert class name to path, as used by %INC.
70
71 # "Foo/Bar.pm"
72 class_to_path 'Foo::Bar';
73
74 # "FooBar.pm"
75 class_to_path 'FooBar';
76
77 decamelize
78 my $snakecase = decamelize $camelcase;
79
80 Convert "CamelCase" string to "snake_case" and replace "::" with "-".
81
82 # "foo_bar"
83 decamelize 'FooBar';
84
85 # "foo_bar-baz"
86 decamelize 'FooBar::Baz';
87
88 # "foo_bar-baz"
89 decamelize 'foo_bar-baz';
90
91 decode
92 my $chars = decode 'UTF-8', $bytes;
93
94 Decode bytes to characters with Encode, or return "undef" if decoding
95 failed.
96
97 deprecated
98 deprecated 'foo is DEPRECATED in favor of bar';
99
100 Warn about deprecated feature from perspective of caller. You can also
101 set the "MOJO_FATAL_DEPRECATIONS" environment variable to make them die
102 instead with Carp.
103
104 dumper
105 my $perl = dumper {some => 'data'};
106
107 Dump a Perl data structure with Data::Dumper.
108
109 encode
110 my $bytes = encode 'UTF-8', $chars;
111
112 Encode characters to bytes with Encode.
113
114 extract_usage
115 my $usage = extract_usage;
116 my $usage = extract_usage '/home/sri/foo.pod';
117
118 Extract usage message from the SYNOPSIS section of a file containing
119 POD documentation, defaults to using the file this function was called
120 from.
121
122 # "Usage: APPLICATION test [OPTIONS]\n"
123 extract_usage;
124
125 =head1 SYNOPSIS
126
127 Usage: APPLICATION test [OPTIONS]
128
129 =cut
130
131 getopt
132 getopt
133 'H|headers=s' => \my @headers,
134 't|timeout=i' => \my $timeout,
135 'v|verbose' => \my $verbose;
136 getopt $array,
137 'H|headers=s' => \my @headers,
138 't|timeout=i' => \my $timeout,
139 'v|verbose' => \my $verbose;
140 getopt $array, ['pass_through'],
141 'H|headers=s' => \my @headers,
142 't|timeout=i' => \my $timeout,
143 'v|verbose' => \my $verbose;
144
145 Extract options from an array reference with Getopt::Long, but without
146 changing its global configuration, defaults to using @ARGV. The
147 configuration options "no_auto_abbrev" and "no_ignore_case" are enabled
148 by default.
149
150 # Extract "charset" option
151 getopt ['--charset', 'UTF-8'], 'charset=s' => \my $charset;
152 say $charset;
153
154 gunzip
155 my $uncompressed = gunzip $compressed;
156
157 Uncompress bytes with IO::Compress::Gunzip.
158
159 gzip
160 my $compressed = gzip $uncompressed;
161
162 Compress bytes with IO::Compress::Gzip.
163
164 hmac_sha1_sum
165 my $checksum = hmac_sha1_sum $bytes, 'passw0rd';
166
167 Generate HMAC-SHA1 checksum for bytes with Digest::SHA.
168
169 # "11cedfd5ec11adc0ec234466d8a0f2a83736aa68"
170 hmac_sha1_sum 'foo', 'passw0rd';
171
172 html_attr_unescape
173 my $str = html_attr_unescape $escaped;
174
175 Same as "html_unescape", but handles special rules from the HTML Living
176 Standard <https://html.spec.whatwg.org> for HTML attributes.
177
178 # "foo=bar<est=baz"
179 html_attr_unescape 'foo=bar<est=baz';
180
181 # "foo=bar<est=baz"
182 html_attr_unescape 'foo=bar<est=baz';
183
184 html_unescape
185 my $str = html_unescape $escaped;
186
187 Unescape all HTML entities in string.
188
189 # "<div>"
190 html_unescape '<div>';
191
192 md5_bytes
193 my $checksum = md5_bytes $bytes;
194
195 Generate binary MD5 checksum for bytes with Digest::MD5.
196
197 md5_sum
198 my $checksum = md5_sum $bytes;
199
200 Generate MD5 checksum for bytes with Digest::MD5.
201
202 # "acbd18db4cc2f85cedef654fccc4a4d8"
203 md5_sum 'foo';
204
205 monkey_patch
206 monkey_patch $package, foo => sub {...};
207 monkey_patch $package, foo => sub {...}, bar => sub {...};
208
209 Monkey patch functions into package.
210
211 monkey_patch 'MyApp',
212 one => sub { say 'One!' },
213 two => sub { say 'Two!' },
214 three => sub { say 'Three!' };
215
216 punycode_decode
217 my $str = punycode_decode $punycode;
218
219 Punycode decode string as described in RFC 3492
220 <http://tools.ietf.org/html/rfc3492>.
221
222 # "bücher"
223 punycode_decode 'bcher-kva';
224
225 punycode_encode
226 my $punycode = punycode_encode $str;
227
228 Punycode encode string as described in RFC 3492
229 <http://tools.ietf.org/html/rfc3492>.
230
231 # "bcher-kva"
232 punycode_encode 'bücher';
233
234 quote
235 my $quoted = quote $str;
236
237 Quote string.
238
239 secure_compare
240 my $bool = secure_compare $str1, $str2;
241
242 Constant time comparison algorithm to prevent timing attacks.
243
244 sha1_bytes
245 my $checksum = sha1_bytes $bytes;
246
247 Generate binary SHA1 checksum for bytes with Digest::SHA.
248
249 sha1_sum
250 my $checksum = sha1_sum $bytes;
251
252 Generate SHA1 checksum for bytes with Digest::SHA.
253
254 # "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"
255 sha1_sum 'foo';
256
257 slugify
258 my $slug = slugify $string;
259 my $slug = slugify $string, $bool;
260
261 Returns a URL slug generated from the input string. Non-word characters
262 are removed, the string is trimmed and lowercased, and whitespace
263 characters are replaced by a dash. By default, non-ASCII characters are
264 normalized to ASCII word characters or removed, but if a true value is
265 passed as the second parameter, all word characters will be allowed in
266 the result according to unicode semantics.
267
268 # "joel-is-a-slug"
269 slugify 'Joel is a slug';
270
271 # "this-is-my-resume"
272 slugify 'This is: my - résumé! ☃ ';
273
274 # "this-is-my-résumé"
275 slugify 'This is: my - résumé! ☃ ', 1;
276
277 split_cookie_header
278 my $tree = split_cookie_header 'a=b; expires=Thu, 07 Aug 2008 07:07:59 GMT';
279
280 Same as "split_header", but handles "expires" values from RFC 6265
281 <http://tools.ietf.org/html/rfc6265>.
282
283 split_header
284 my $tree = split_header 'foo="bar baz"; test=123, yada';
285
286 Split HTTP header value into key/value pairs, each comma separated part
287 gets its own array reference, and keys without a value get "undef"
288 assigned.
289
290 # "one"
291 split_header('one; two="three four", five=six')->[0][0];
292
293 # "two"
294 split_header('one; two="three four", five=six')->[0][2];
295
296 # "three four"
297 split_header('one; two="three four", five=six')->[0][3];
298
299 # "five"
300 split_header('one; two="three four", five=six')->[1][0];
301
302 # "six"
303 split_header('one; two="three four", five=six')->[1][1];
304
305 steady_time
306 my $time = steady_time;
307
308 High resolution time elapsed from an arbitrary fixed point in the past,
309 resilient to time jumps if a monotonic clock is available through
310 Time::HiRes.
311
312 tablify
313 my $table = tablify [['foo', 'bar'], ['baz', 'yada']];
314
315 Row-oriented generator for text tables.
316
317 # "foo bar\nyada yada\nbaz yada\n"
318 tablify [['foo', 'bar'], ['yada', 'yada'], ['baz', 'yada']];
319
320 term_escape
321 my $escaped = term_escape $str;
322
323 Escape all POSIX control characters except for "\n".
324
325 # "foo\\x09bar\\x0d\n"
326 term_escape "foo\tbar\r\n";
327
328 trim
329 my $trimmed = trim $str;
330
331 Trim whitespace characters from both ends of string.
332
333 # "foo bar"
334 trim ' foo bar ';
335
336 unindent
337 my $unindented = unindent $str;
338
339 Unindent multi-line string.
340
341 # "foo\nbar\nbaz\n"
342 unindent " foo\n bar\n baz\n";
343
344 unquote
345 my $str = unquote $quoted;
346
347 Unquote string.
348
349 url_escape
350 my $escaped = url_escape $str;
351 my $escaped = url_escape $str, '^A-Za-z0-9\-._~';
352
353 Percent encode unsafe characters in string as described in RFC 3986
354 <http://tools.ietf.org/html/rfc3986>, the pattern used defaults to
355 "^A-Za-z0-9\-._~".
356
357 # "foo%3Bbar"
358 url_escape 'foo;bar';
359
360 url_unescape
361 my $str = url_unescape $escaped;
362
363 Decode percent encoded characters in string as described in RFC 3986
364 <http://tools.ietf.org/html/rfc3986>.
365
366 # "foo;bar"
367 url_unescape 'foo%3Bbar';
368
369 xml_escape
370 my $escaped = xml_escape $str;
371
372 Escape unsafe characters "&", "<", ">", """ and "'" in string, but do
373 not escape Mojo::ByteStream objects.
374
375 # "<div>"
376 xml_escape '<div>';
377
378 # "<div>"
379 use Mojo::ByteStream 'b';
380 xml_escape b('<div>');
381
382 xor_encode
383 my $encoded = xor_encode $str, $key;
384
385 XOR encode string with variable length key.
386
388 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
389
390
391
392perl v5.28.1 2019-01-27 Mojo::Util(3)