NAME
Mojo::Util - Portable utility functions
SYNOPSIS
use Mojo::Util qw(b64_encode url_escape url_unescape) ; my $str = 'test=23' ;
my $escaped = url_escape $str ;
say url_unescape $escaped ;
say b64_encode $escaped , '' ;
|
DESCRIPTION
Mojo::Util provides portable utility functions for Mojo.
FUNCTIONS
Mojo::Util implements the following functions, which can be imported individually.
b64_decode
my $bytes = b64_decode $b64 ;
|
Base64 decode bytes with MIME::Base64.
b64_encode
my $b64 = b64_encode $bytes ;
my $b64 = b64_encode $bytes , "\n" ;
|
Base64 encode bytes with MIME::Base64, the line ending defaults to a newline.
camelize
my $camelcase = camelize $snakecase ;
|
Convert snake_case
string to CamelCase
and replace -
with ::
.
camelize 'foo_bar' ;
camelize 'foo_bar-baz' ;
camelize 'FooBar::Baz' ;
|
class_to_file
my $file = class_to_file 'Foo::Bar' ;
|
Convert a class name to a file.
class_to_file 'Foo::Bar' ;
class_to_file 'FOO::Bar' ;
class_to_file 'FooBar' ;
class_to_file 'FOOBar' ;
|
class_to_path
my $path = class_to_path 'Foo::Bar' ;
|
Convert class name to path, as used by %INC
.
class_to_path 'Foo::Bar' ;
class_to_path 'FooBar' ;
|
decamelize
my $snakecase = decamelize $camelcase ;
|
Convert CamelCase
string to snake_case
and replace ::
with -
.
decamelize 'FooBar' ;
decamelize 'FooBar::Baz' ;
decamelize 'foo_bar-baz' ;
|
decode
my $chars = decode 'UTF-8' , $bytes ;
|
Decode bytes to characters with Encode, or return undef
if decoding failed.
decrypt_cookie
my $value = decrypt_cookie $encrypted , 'passw0rd' , 'salt' ;
|
Decrypt cookie value encrypted with "encrypt_cookie", returns the decrypted value or undef
. Note that this function is EXPERIMENTAL and might change without warning!
deprecated
deprecated 'foo is DEPRECATED in favor of bar' ;
|
Warn about deprecated feature from perspective of caller. You can also set the MOJO_FATAL_DEPRECATIONS
environment variable to make them die instead with Carp.
dumper
my $perl = dumper { some => 'data' };
|
Dump a Perl data structure with Data::Dumper.
encode
my $bytes = encode 'UTF-8' , $chars ;
|
Encode characters to bytes with Encode.
encrypt_cookie
my $encrypted = encrypt_cookie $value , 'passw0rd' , 'salt' ;
|
Encrypt cookie value. Note that this function is EXPERIMENTAL and might change without warning!
my $usage = extract_usage;
my $usage = extract_usage '/home/sri/foo.pod' ;
|
Extract usage message from the SYNOPSIS section of a file containing POD documentation, defaults to using the file this function was called from.
generate_secret
my $secret = generate_secret;
|
Generate a random secret with a cryptographically secure random number generator if available, and a less secure fallback if not. Note that this function is EXPERIMENTAL and might change without warning!
getopt
getopt
'H|headers=s' => \ my @headers ,
't|timeout=i' => \ my $timeout ,
'v|verbose' => \ my $verbose ;
getopt $array ,
'H|headers=s' => \ my @headers ,
't|timeout=i' => \ my $timeout ,
'v|verbose' => \ my $verbose ;
getopt $array , [ 'pass_through' ],
'H|headers=s' => \ my @headers ,
't|timeout=i' => \ my $timeout ,
'v|verbose' => \ my $verbose ;
|
Extract options from an array reference with Getopt::Long, but without changing its global configuration, defaults to using @ARGV
. The configuration options no_auto_abbrev
and no_ignore_case
are enabled by default.
getopt [ '--charset' , 'UTF-8' ], 'charset=s' => \ my $charset ;
say $charset ;
|
gunzip
my $uncompressed = gunzip $compressed ;
|
Uncompress bytes with IO::Compress::Gunzip.
gzip
my $compressed = gzip $uncompressed ;
|
Compress bytes with IO::Compress::Gzip.
my ( $params , $remainder ) = header_params 'one=foo; two="bar", three=baz' ;
|
Extract HTTP header field parameters until the first comma according to RFC 5987. Note that this function is EXPERIMENTAL and might change without warning!
hmac_sha1_sum
my $checksum = hmac_sha1_sum $bytes , 'passw0rd' ;
|
Generate HMAC-SHA1 checksum for bytes with Digest::SHA.
hmac_sha1_sum 'foo' , 'passw0rd' ;
|
html_attr_unescape
my $str = html_attr_unescape $escaped ;
|
Same as "html_unescape", but handles special rules from the HTML Living Standard for HTML attributes.
html_attr_unescape 'foo=bar<est=baz' ;
html_attr_unescape 'foo=bar<est=baz' ;
|
html_unescape
my $str = html_unescape $escaped ;
|
Unescape all HTML entities in string.
html_unescape '<div>' ;
|
humanize_bytes
my $str = humanize_bytes 1234;
|
Turn number of bytes into a simplified human readable format.
humanize_bytes 1;
humanize_bytes 8007188480;
humanize_bytes 13443399680;
humanize_bytes -717946880;
|
md5_bytes
my $checksum = md5_bytes $bytes ;
|
Generate binary MD5 checksum for bytes with Digest::MD5.
md5_sum
my $checksum = md5_sum $bytes ;
|
Generate MD5 checksum for bytes with Digest::MD5.
monkey_patch
monkey_patch $package , foo => sub {...};
monkey_patch $package , foo => sub {...}, bar => sub {...};
|
Monkey patch functions into package.
monkey_patch 'MyApp' ,
one => sub { say 'One!' },
two => sub { say 'Two!' },
three => sub { say 'Three!' };
|
punycode_decode
my $str = punycode_decode $punycode ;
|
Punycode decode string as described in RFC 3492.
punycode_decode 'bcher-kva' ;
|
network_contains
my $bool = network_contains $network , $address ;
|
Check that a given address is contained within a network in CIDR form. If the network is a single address, the addresses must be equivalent.
network_contains( '10.0.0.0/8' , '10.10.10.10' );
network_contains( '10.10.10.10' , '10.10.10.10' );
network_contains( 'fc00::/7' , 'fc::c0:ff:ee' );
network_contains( '10.0.0.0/29' , '10.10.10.10' );
network_contains( '10.10.10.12' , '10.10.10.10' );
network_contains( 'fc00::/7' , '::1' );
|
punycode_encode
my $punycode = punycode_encode $str ;
|
Punycode encode string as described in RFC 3492.
punycode_encode 'bücher' ;
|
quote
Quote string.
scope_guard
my $guard = scope_guard sub {...};
|
Create anonymous scope guard object that will execute the passed callback when the object is destroyed.
{
my $guard = scope_guard sub { say "Mojo!" };
say "Hello" ;
}
|
secure_compare
my $bool = secure_compare $str1 , $str2 ;
|
Constant time comparison algorithm to prevent timing attacks. The secret string should be the second argument, to avoid leaking information about the length of the string.
sha1_bytes
my $checksum = sha1_bytes $bytes ;
|
Generate binary SHA1 checksum for bytes with Digest::SHA.
sha1_sum
my $checksum = sha1_sum $bytes ;
|
Generate SHA1 checksum for bytes with Digest::SHA.
slugify
my $slug = slugify $string ;
my $slug = slugify $string , $bool ;
|
Returns a URL slug generated from the input string. Non-word characters are removed, the string is trimmed and lowercased, and whitespace characters are replaced by a dash. By default, non-ASCII characters are normalized to ASCII word characters or removed, but if a true value is passed as the second parameter, all word characters will be allowed in the result according to unicode semantics.
slugify 'Joel is a slug' ;
slugify 'This is: my - résumé! ☃ ' ;
slugify 'This is: my - résumé! ☃ ' , 1;
|
my $tree = split_cookie_header 'a=b; expires=Thu, 07 Aug 2008 07:07:59 GMT' ;
|
Same as "split_header", but handles expires
values from RFC 6265.
my $tree = split_header 'foo="bar baz"; test=123, yada' ;
|
Split HTTP header value into key/value pairs, each comma separated part gets its own array reference, and keys without a value get undef
assigned.
split_header( 'one; two="three four", five=six' )->[0][0];
split_header( 'one; two="three four", five=six' )->[0][2];
split_header( 'one; two="three four", five=six' )->[0][3];
split_header( 'one; two="three four", five=six' )->[1][0];
split_header( 'one; two="three four", five=six' )->[1][1];
|
steady_time
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.
tablify
my $table = tablify [[ 'foo' , 'bar' ], [ 'baz' , 'yada' ]];
|
Row-oriented generator for text tables.
tablify [[ 'foo' , 'bar' ], [ 'yada' , 'yada' ], [ 'baz' , 'yada' ]];
|
term_escape
my $escaped = term_escape $str ;
|
Escape all POSIX control characters except for \n
.
term_escape "foo\tbar\r\n" ;
|
trim
Trim whitespace characters from both ends of string.
unindent
my $unindented = unindent $str ;
|
Unindent multi-line string.
unindent " foo\n bar\n baz\n" ;
|
unquote
my $str = unquote $quoted ;
|
Unquote string.
url_escape
my $escaped = url_escape $str ;
my $escaped = url_escape $str , '^A-Za-z0-9\-._~' ;
|
Percent encode unsafe characters in string as described in RFC 3986, the pattern used defaults to ^A-Za-z0-9\-._~
.
url_unescape
my $str = url_unescape $escaped ;
|
Decode percent encoded characters in string as described in RFC 3986.
url_unescape 'foo%3Bbar' ;
|
xml_escape
my $escaped = xml_escape $str ;
|
Escape unsafe characters &
, <
, >
, "
and '
in string, but do not escape Mojo::ByteStream objects.
xml_escape '<div>' ;
xml_escape b( '<div>' );
|
xor_encode
my $encoded = xor_encode $str , $key ;
|
XOR encode string with variable length key.
SEE ALSO
Mojolicious, Mojolicious::Guides, https://mojolicious.org.