1Mojo::Util(3)         User Contributed Perl Documentation        Mojo::Util(3)
2
3
4

NAME

6       Mojo::Util - Portable utility functions
7

SYNOPSIS

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

DESCRIPTION

17       Mojo::Util provides portable utility functions for Mojo.
18

FUNCTIONS

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&ltest=baz"
179         html_attr_unescape 'foo=bar&ltest=baz';
180
181         # "foo=bar<est=baz"
182         html_attr_unescape 'foo=bar&lt;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 '&lt;div&gt;';
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   scope_guard
240         my $guard = scope_guard sub {...};
241
242       Create anonymous scope guard object that will execute the passed
243       callback when the object is destroyed. Note that this function is
244       EXPERIMENTAL and might change without warning
245
246         # Execute closure at end of scope
247         {
248           my $guard = scope_guard sub { say "Mojo!" };
249           say "Hello";
250         }
251
252   secure_compare
253         my $bool = secure_compare $str1, $str2;
254
255       Constant time comparison algorithm to prevent timing attacks.
256
257   sha1_bytes
258         my $checksum = sha1_bytes $bytes;
259
260       Generate binary SHA1 checksum for bytes with Digest::SHA.
261
262   sha1_sum
263         my $checksum = sha1_sum $bytes;
264
265       Generate SHA1 checksum for bytes with Digest::SHA.
266
267         # "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"
268         sha1_sum 'foo';
269
270   slugify
271         my $slug = slugify $string;
272         my $slug = slugify $string, $bool;
273
274       Returns a URL slug generated from the input string. Non-word characters
275       are removed, the string is trimmed and lowercased, and whitespace
276       characters are replaced by a dash. By default, non-ASCII characters are
277       normalized to ASCII word characters or removed, but if a true value is
278       passed as the second parameter, all word characters will be allowed in
279       the result according to unicode semantics.
280
281         # "joel-is-a-slug"
282         slugify 'Joel is a slug';
283
284         # "this-is-my-resume"
285         slugify 'This is: my - résumé! ☃ ';
286
287         # "this-is-my-résumé"
288         slugify 'This is: my - résumé! ☃ ', 1;
289
290   split_cookie_header
291         my $tree = split_cookie_header 'a=b; expires=Thu, 07 Aug 2008 07:07:59 GMT';
292
293       Same as "split_header", but handles "expires" values from RFC 6265
294       <http://tools.ietf.org/html/rfc6265>.
295
296   split_header
297          my $tree = split_header 'foo="bar baz"; test=123, yada';
298
299       Split HTTP header value into key/value pairs, each comma separated part
300       gets its own array reference, and keys without a value get "undef"
301       assigned.
302
303         # "one"
304         split_header('one; two="three four", five=six')->[0][0];
305
306         # "two"
307         split_header('one; two="three four", five=six')->[0][2];
308
309         # "three four"
310         split_header('one; two="three four", five=six')->[0][3];
311
312         # "five"
313         split_header('one; two="three four", five=six')->[1][0];
314
315         # "six"
316         split_header('one; two="three four", five=six')->[1][1];
317
318   steady_time
319         my $time = steady_time;
320
321       High resolution time elapsed from an arbitrary fixed point in the past,
322       resilient to time jumps if a monotonic clock is available through
323       Time::HiRes.
324
325   tablify
326         my $table = tablify [['foo', 'bar'], ['baz', 'yada']];
327
328       Row-oriented generator for text tables.
329
330         # "foo   bar\nyada  yada\nbaz   yada\n"
331         tablify [['foo', 'bar'], ['yada', 'yada'], ['baz', 'yada']];
332
333   term_escape
334         my $escaped = term_escape $str;
335
336       Escape all POSIX control characters except for "\n".
337
338         # "foo\\x09bar\\x0d\n"
339         term_escape "foo\tbar\r\n";
340
341   trim
342         my $trimmed = trim $str;
343
344       Trim whitespace characters from both ends of string.
345
346         # "foo bar"
347         trim '  foo bar  ';
348
349   unindent
350         my $unindented = unindent $str;
351
352       Unindent multi-line string.
353
354         # "foo\nbar\nbaz\n"
355         unindent "  foo\n  bar\n  baz\n";
356
357   unquote
358         my $str = unquote $quoted;
359
360       Unquote string.
361
362   url_escape
363         my $escaped = url_escape $str;
364         my $escaped = url_escape $str, '^A-Za-z0-9\-._~';
365
366       Percent encode unsafe characters in string as described in RFC 3986
367       <http://tools.ietf.org/html/rfc3986>, the pattern used defaults to
368       "^A-Za-z0-9\-._~".
369
370         # "foo%3Bbar"
371         url_escape 'foo;bar';
372
373   url_unescape
374         my $str = url_unescape $escaped;
375
376       Decode percent encoded characters in string as described in RFC 3986
377       <http://tools.ietf.org/html/rfc3986>.
378
379         # "foo;bar"
380         url_unescape 'foo%3Bbar';
381
382   xml_escape
383         my $escaped = xml_escape $str;
384
385       Escape unsafe characters "&", "<", ">", """ and "'" in string, but do
386       not escape Mojo::ByteStream objects.
387
388         # "&lt;div&gt;"
389         xml_escape '<div>';
390
391         # "<div>"
392         use Mojo::ByteStream 'b';
393         xml_escape b('<div>');
394
395   xor_encode
396         my $encoded = xor_encode $str, $key;
397
398       XOR encode string with variable length key.
399

SEE ALSO

401       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
402
403
404
405perl v5.30.1                      2020-01-30                     Mojo::Util(3)
Impressum