.\" Automatically generated by Pod::Man 4.07 (Pod::Simple 3.32) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Mojo::Util 3pm" .TH Mojo::Util 3pm "2017-01-24" "perl v5.24.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Mojo::Util \- Portable utility functions .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Mojo::Util qw(b64_encode url_escape url_unescape); \& \& my $str = \*(Aqtest=23\*(Aq; \& my $escaped = url_escape $str; \& say url_unescape $escaped; \& say b64_encode $escaped, \*(Aq\*(Aq; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Mojo::Util provides portable utility functions for Mojo. .SH "FUNCTIONS" .IX Header "FUNCTIONS" Mojo::Util implements the following functions, which can be imported individually. .SS "b64_decode" .IX Subsection "b64_decode" .Vb 1 \& my $bytes = b64_decode $b64; .Ve .PP Base64 decode bytes. .SS "b64_encode" .IX Subsection "b64_encode" .Vb 2 \& my $b64 = b64_encode $bytes; \& my $b64 = b64_encode $bytes, "\en"; .Ve .PP Base64 encode bytes, the line ending defaults to a newline. .SS "camelize" .IX Subsection "camelize" .Vb 1 \& my $camelcase = camelize $snakecase; .Ve .PP Convert \f(CW\*(C`snake_case\*(C'\fR string to \f(CW\*(C`CamelCase\*(C'\fR and replace \f(CW\*(C`\-\*(C'\fR with \f(CW\*(C`::\*(C'\fR. .PP .Vb 2 \& # "FooBar" \& camelize \*(Aqfoo_bar\*(Aq; \& \& # "FooBar::Baz" \& camelize \*(Aqfoo_bar\-baz\*(Aq; \& \& # "FooBar::Baz" \& camelize \*(AqFooBar::Baz\*(Aq; .Ve .SS "class_to_file" .IX Subsection "class_to_file" .Vb 1 \& my $file = class_to_file \*(AqFoo::Bar\*(Aq; .Ve .PP Convert a class name to a file. .PP .Vb 2 \& # "foo_bar" \& class_to_file \*(AqFoo::Bar\*(Aq; \& \& # "foobar" \& class_to_file \*(AqFOO::Bar\*(Aq; \& \& # "foo_bar" \& class_to_file \*(AqFooBar\*(Aq; \& \& # "foobar" \& class_to_file \*(AqFOOBar\*(Aq; .Ve .SS "class_to_path" .IX Subsection "class_to_path" .Vb 1 \& my $path = class_to_path \*(AqFoo::Bar\*(Aq; .Ve .PP Convert class name to path, as used by \f(CW%INC\fR. .PP .Vb 2 \& # "Foo/Bar.pm" \& class_to_path \*(AqFoo::Bar\*(Aq; \& \& # "FooBar.pm" \& class_to_path \*(AqFooBar\*(Aq; .Ve .SS "decamelize" .IX Subsection "decamelize" .Vb 1 \& my $snakecase = decamelize $camelcase; .Ve .PP Convert \f(CW\*(C`CamelCase\*(C'\fR string to \f(CW\*(C`snake_case\*(C'\fR and replace \f(CW\*(C`::\*(C'\fR with \f(CW\*(C`\-\*(C'\fR. .PP .Vb 2 \& # "foo_bar" \& decamelize \*(AqFooBar\*(Aq; \& \& # "foo_bar\-baz" \& decamelize \*(AqFooBar::Baz\*(Aq; \& \& # "foo_bar\-baz" \& decamelize \*(Aqfoo_bar\-baz\*(Aq; .Ve .SS "decode" .IX Subsection "decode" .Vb 1 \& my $chars = decode \*(AqUTF\-8\*(Aq, $bytes; .Ve .PP Decode bytes to characters, or return \f(CW\*(C`undef\*(C'\fR if decoding failed. .SS "deprecated" .IX Subsection "deprecated" .Vb 1 \& deprecated \*(Aqfoo is DEPRECATED in favor of bar\*(Aq; .Ve .PP Warn about deprecated feature from perspective of caller. You can also set the \&\f(CW\*(C`MOJO_FATAL_DEPRECATIONS\*(C'\fR environment variable to make them die instead. .SS "dumper" .IX Subsection "dumper" .Vb 1 \& my $perl = dumper {some => \*(Aqdata\*(Aq}; .Ve .PP Dump a Perl data structure with Data::Dumper. .SS "encode" .IX Subsection "encode" .Vb 1 \& my $bytes = encode \*(AqUTF\-8\*(Aq, $chars; .Ve .PP Encode characters to bytes. .SS "extract_usage" .IX Subsection "extract_usage" .Vb 2 \& my $usage = extract_usage; \& my $usage = extract_usage \*(Aq/home/sri/foo.pod\*(Aq; .Ve .PP Extract usage message from the \s-1SYNOPSIS\s0 section of a file containing \s-1POD\s0 documentation, defaults to using the file this function was called from. .PP .Vb 2 \& # "Usage: APPLICATION test [OPTIONS]\en" \& extract_usage; \& \& =head1 SYNOPSIS \& \& Usage: APPLICATION test [OPTIONS] \& \& =cut .Ve .SS "getopt" .IX Subsection "getopt" .Vb 12 \& getopt \& \*(AqH|headers=s\*(Aq => \emy @headers, \& \*(Aqt|timeout=i\*(Aq => \emy $timeout, \& \*(Aqv|verbose\*(Aq => \emy $verbose; \& getopt $array, \& \*(AqH|headers=s\*(Aq => \emy @headers, \& \*(Aqt|timeout=i\*(Aq => \emy $timeout, \& \*(Aqv|verbose\*(Aq => \emy $verbose; \& getopt $array, [\*(Aqpass_through\*(Aq], \& \*(AqH|headers=s\*(Aq => \emy @headers, \& \*(Aqt|timeout=i\*(Aq => \emy $timeout, \& \*(Aqv|verbose\*(Aq => \emy $verbose; .Ve .PP Extract options from an array reference with Getopt::Long, but without changing its global configuration, defaults to using \f(CW@ARGV\fR. The configuration options \f(CW\*(C`no_auto_abbrev\*(C'\fR and \f(CW\*(C`no_ignore_case\*(C'\fR are enabled by default. .PP .Vb 3 \& # Extract "charset" option \& getopt [\*(Aq\-\-charset\*(Aq, \*(AqUTF\-8\*(Aq], \*(Aqcharset=s\*(Aq => \emy $charset; \& say $charset; .Ve .SS "hmac_sha1_sum" .IX Subsection "hmac_sha1_sum" .Vb 1 \& my $checksum = hmac_sha1_sum $bytes, \*(Aqpassw0rd\*(Aq; .Ve .PP Generate \s-1HMAC\-SHA1\s0 checksum for bytes. .PP .Vb 2 \& # "11cedfd5ec11adc0ec234466d8a0f2a83736aa68" \& hmac_sha1_sum \*(Aqfoo\*(Aq, \*(Aqpassw0rd\*(Aq; .Ve .SS "html_unescape" .IX Subsection "html_unescape" .Vb 1 \& my $str = html_unescape $escaped; .Ve .PP Unescape all \s-1HTML\s0 entities in string. .PP .Vb 2 \& # "
" \& html_unescape \*(Aq<div>\*(Aq; .Ve .SS "md5_bytes" .IX Subsection "md5_bytes" .Vb 1 \& my $checksum = md5_bytes $bytes; .Ve .PP Generate binary \s-1MD5\s0 checksum for bytes. .SS "md5_sum" .IX Subsection "md5_sum" .Vb 1 \& my $checksum = md5_sum $bytes; .Ve .PP Generate \s-1MD5\s0 checksum for bytes. .PP .Vb 2 \& # "acbd18db4cc2f85cedef654fccc4a4d8" \& md5_sum \*(Aqfoo\*(Aq; .Ve .SS "monkey_patch" .IX Subsection "monkey_patch" .Vb 2 \& monkey_patch $package, foo => sub {...}; \& monkey_patch $package, foo => sub {...}, bar => sub {...}; .Ve .PP Monkey patch functions into package. .PP .Vb 4 \& monkey_patch \*(AqMyApp\*(Aq, \& one => sub { say \*(AqOne!\*(Aq }, \& two => sub { say \*(AqTwo!\*(Aq }, \& three => sub { say \*(AqThree!\*(Aq }; .Ve .SS "punycode_decode" .IX Subsection "punycode_decode" .Vb 1 \& my $str = punycode_decode $punycode; .Ve .PP Punycode decode string as described in \&\s-1RFC 3492\s0 . .PP .Vb 2 \& # "bücher" \& punycode_decode \*(Aqbcher\-kva\*(Aq; .Ve .SS "punycode_encode" .IX Subsection "punycode_encode" .Vb 1 \& my $punycode = punycode_encode $str; .Ve .PP Punycode encode string as described in \&\s-1RFC 3492\s0 . .PP .Vb 2 \& # "bcher\-kva" \& punycode_encode \*(Aqbücher\*(Aq; .Ve .SS "quote" .IX Subsection "quote" .Vb 1 \& my $quoted = quote $str; .Ve .PP Quote string. .SS "secure_compare" .IX Subsection "secure_compare" .Vb 1 \& my $bool = secure_compare $str1, $str2; .Ve .PP Constant time comparison algorithm to prevent timing attacks. .SS "sha1_bytes" .IX Subsection "sha1_bytes" .Vb 1 \& my $checksum = sha1_bytes $bytes; .Ve .PP Generate binary \s-1SHA1\s0 checksum for bytes. .SS "sha1_sum" .IX Subsection "sha1_sum" .Vb 1 \& my $checksum = sha1_sum $bytes; .Ve .PP Generate \s-1SHA1\s0 checksum for bytes. .PP .Vb 2 \& # "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33" \& sha1_sum \*(Aqfoo\*(Aq; .Ve .SS "split_cookie_header" .IX Subsection "split_cookie_header" .Vb 1 \& my $tree = split_cookie_header \*(Aqa=b; expires=Thu, 07 Aug 2008 07:07:59 GMT\*(Aq; .Ve .PP Same as \*(L"split_header\*(R", but handles \f(CW\*(C`expires\*(C'\fR values from \&\s-1RFC 6265\s0 . .SS "split_header" .IX Subsection "split_header" .Vb 1 \& my $tree = split_header \*(Aqfoo="bar baz"; test=123, yada\*(Aq; .Ve .PP Split \s-1HTTP\s0 header value into key/value pairs, each comma separated part gets its own array reference, and keys without a value get \f(CW\*(C`undef\*(C'\fR assigned. .PP .Vb 2 \& # "one" \& split_header(\*(Aqone; two="three four", five=six\*(Aq)\->[0][0]; \& \& # "two" \& split_header(\*(Aqone; two="three four", five=six\*(Aq)\->[0][2]; \& \& # "three four" \& split_header(\*(Aqone; two="three four", five=six\*(Aq)\->[0][3]; \& \& # "five" \& split_header(\*(Aqone; two="three four", five=six\*(Aq)\->[1][0]; \& \& # "six" \& split_header(\*(Aqone; two="three four", five=six\*(Aq)\->[1][1]; .Ve .SS "steady_time" .IX Subsection "steady_time" .Vb 1 \& my $time = steady_time; .Ve .PP High resolution time elapsed from an arbitrary fixed point in the past, resilient to time jumps if a monotonic clock is available through Time::HiRes. .SS "tablify" .IX Subsection "tablify" .Vb 1 \& my $table = tablify [[\*(Aqfoo\*(Aq, \*(Aqbar\*(Aq], [\*(Aqbaz\*(Aq, \*(Aqyada\*(Aq]]; .Ve .PP Row-oriented generator for text tables. .PP .Vb 2 \& # "foo bar\enyada yada\enbaz yada\en" \& tablify [[\*(Aqfoo\*(Aq, \*(Aqbar\*(Aq], [\*(Aqyada\*(Aq, \*(Aqyada\*(Aq], [\*(Aqbaz\*(Aq, \*(Aqyada\*(Aq]]; .Ve .SS "term_escape" .IX Subsection "term_escape" .Vb 1 \& my $escaped = term_escape $str; .Ve .PP Escape all \s-1POSIX\s0 control characters except for \f(CW\*(C`\en\*(C'\fR. .PP .Vb 2 \& # "foo\e\ex09bar\e\ex0d\en" \& term_escape "foo\etbar\er\en"; .Ve .SS "trim" .IX Subsection "trim" .Vb 1 \& my $trimmed = trim $str; .Ve .PP Trim whitespace characters from both ends of string. .PP .Vb 2 \& # "foo bar" \& trim \*(Aq foo bar \*(Aq; .Ve .SS "unindent" .IX Subsection "unindent" .Vb 1 \& my $unindented = unindent $str; .Ve .PP Unindent multi-line string. .PP .Vb 2 \& # "foo\enbar\enbaz\en" \& unindent " foo\en bar\en baz\en"; .Ve .SS "unquote" .IX Subsection "unquote" .Vb 1 \& my $str = unquote $quoted; .Ve .PP Unquote string. .SS "url_escape" .IX Subsection "url_escape" .Vb 2 \& my $escaped = url_escape $str; \& my $escaped = url_escape $str, \*(Aq^A\-Za\-z0\-9\e\-._~\*(Aq; .Ve .PP Percent encode unsafe characters in string as described in \&\s-1RFC 3986\s0 , the pattern used defaults to \&\f(CW\*(C`^A\-Za\-z0\-9\e\-._~\*(C'\fR. .PP .Vb 2 \& # "foo%3Bbar" \& url_escape \*(Aqfoo;bar\*(Aq; .Ve .SS "url_unescape" .IX Subsection "url_unescape" .Vb 1 \& my $str = url_unescape $escaped; .Ve .PP Decode percent encoded characters in string as described in \&\s-1RFC 3986\s0 . .PP .Vb 2 \& # "foo;bar" \& url_unescape \*(Aqfoo%3Bbar\*(Aq; .Ve .SS "xml_escape" .IX Subsection "xml_escape" .Vb 1 \& my $escaped = xml_escape $str; .Ve .PP Escape unsafe characters \f(CW\*(C`&\*(C'\fR, \f(CW\*(C`<\*(C'\fR, \f(CW\*(C`>\*(C'\fR, \f(CW\*(C`"\*(C'\fR and \f(CW\*(C`\*(Aq\*(C'\fR in string, but do not escape Mojo::ByteStream objects. .PP .Vb 2 \& # "<div>" \& xml_escape \*(Aq
\*(Aq; \& \& # "
" \& use Mojo::ByteStream \*(Aqb\*(Aq; \& xml_escape b(\*(Aq
\*(Aq); .Ve .SS "xor_encode" .IX Subsection "xor_encode" .Vb 1 \& my $encoded = xor_encode $str, $key; .Ve .PP \&\s-1XOR\s0 encode string with variable length key. .SH "SEE ALSO" .IX Header "SEE ALSO" Mojolicious, Mojolicious::Guides, .