Mojo::Util - Portable utility functions
use Mojo::Util qw(b64_encode url_escape url_unescape); my $string = 'test=23'; my $escaped = url_escape $string; say url_unescape $escaped; say b64_encode $escaped, '';
Mojo::Util provides portable utility functions for Mojo.
Mojo::Util implements the following functions.
my $string = b64_decode $b64;
Base64 decode string.
my $b64 = b64_encode $string; my $b64 = b64_encode $string, "\n";
Base64 encode string, the line ending defaults to a newline.
my $camelcase = camelize $snakecase;
Convert snake case string to camel case and replace - with ::.
-
::
# "FooBar" camelize 'foo_bar'; # "FooBar::Baz" camelize 'foo_bar-baz'; # "FooBar::Baz" camelize 'FooBar::Baz';
my $file = class_to_file 'Foo::Bar';
Convert a class name to a file.
Foo::Bar -> foo_bar FOO::Bar -> foobar FooBar -> foo_bar FOOBar -> foobar
my $path = class_to_path 'Foo::Bar';
Convert class name to path.
Foo::Bar -> Foo/Bar.pm FooBar -> FooBar.pm
my $snakecase = decamelize $camelcase;
Convert camel case string to snake case and replace :: with -.
# "foo_bar" decamelize 'FooBar'; # "foo_bar-baz" decamelize 'FooBar::Baz'; # "foo_bar-baz" decamelize 'foo_bar-baz';
my $chars = decode 'UTF-8', $bytes;
Decode bytes to characters and return undef if decoding failed.
undef
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.
my $bytes = encode 'UTF-8', $chars;
Encode characters to bytes.
my $line = get_line \$string;
Extract whole line from string or return undef. Lines are expected to end with 0x0d 0x0a or 0x0a.
0x0d 0x0a
0x0a
my $checksum = hmac_md5_sum $string, 'passw0rd';
Generate HMAC-MD5 checksum for string.
my $checksum = hmac_sha1_sum $string, 'passw0rd';
Generate HMAC-SHA1 checksum for string.
my $string = html_unescape $escaped;
Unescape all HTML entities in string.
my $checksum = md5_bytes $string;
Generate binary MD5 checksum for string.
my $checksum = md5_sum $string;
Generate MD5 checksum for string.
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!' };
my $string = punycode_decode $punycode;
Punycode decode string.
my $punycode = punycode_encode $string;
Punycode encode string.
my $quoted = quote $string;
Quote string.
my $success = secure_compare $string1, $string2;
Constant time comparison algorithm to prevent timing attacks.
my $checksum = sha1_bytes $string;
Generate binary SHA1 checksum for string.
my $checksum = sha1_sum $string;
Generate SHA1 checksum for string.
my $content = slurp '/etc/passwd';
Read all data at once from file.
$content = spurt $content, '/etc/passwd';
Write all data at once to file.
my $squished = squish $string;
Trim whitespace characters from both ends of string and then change all consecutive groups of whitespace into one space each.
my $trimmed = trim $string;
Trim whitespace characters from both ends of string.
my $string = unquote $quoted;
Unquote string.
my $escaped = url_escape $string; my $escaped = url_escape $string, '^A-Za-z0-9\-._~';
Percent encode unsafe characters in string, the pattern used defaults to ^A-Za-z0-9\-._~.
^A-Za-z0-9\-._~
my $string = url_unescape $escaped;
Decode percent encoded characters in string.
my $escaped = xml_escape $string;
Escape unsafe characters &, <, >, " and ' in string.
&
<
>
"
'
my $encoded = xor_encode $string, $key;
XOR encode string with variable length key.
Mojolicious, Mojolicious::Guides, http://mojolicio.us.
To install Mojolicious, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious
CPAN shell
perl -MCPAN -e shell install Mojolicious
For more information on module installation, please visit the detailed CPAN module installation guide.