GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Mojo::Util(3) User Contributed Perl Documentation Mojo::Util(3)
 

Mojo::Util - Portable utility functions

  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, '';

Mojo::Util provides portable utility functions for Mojo.

Mojo::Util implements the following functions, which can be imported individually.

  my $bytes = b64_decode $b64;
Base64 decode bytes with MIME::Base64.

  my $b64 = b64_encode $bytes;
  my $b64 = b64_encode $bytes, "\n";
Base64 encode bytes with MIME::Base64, the line ending defaults to a newline.

  my $camelcase = camelize $snakecase;
Convert "snake_case" string to "CamelCase" 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"
  class_to_file 'Foo::Bar';
  # "foobar"
  class_to_file 'FOO::Bar';
  # "foo_bar"
  class_to_file 'FooBar';
  # "foobar"
  class_to_file 'FOOBar';

  my $path = class_to_path 'Foo::Bar';
Convert class name to path, as used by %INC.
  # "Foo/Bar.pm"
  class_to_path 'Foo::Bar';
  # "FooBar.pm"
  class_to_path 'FooBar';

  my $snakecase = decamelize $camelcase;
Convert "CamelCase" 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 with Encode, or return "undef" if decoding failed.

  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 $perl = dumper {some => 'data'};
Dump a Perl data structure with Data::Dumper.

  my $bytes = encode 'UTF-8', $chars;
Encode characters to bytes with Encode.

  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.
  # "Usage: APPLICATION test [OPTIONS]\n"
  extract_usage;
  =head1 SYNOPSIS
    Usage: APPLICATION test [OPTIONS]
  =cut

  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.
  # Extract "charset" option
  getopt ['--charset', 'UTF-8'], 'charset=s' => \my $charset;
  say $charset;

  my $uncompressed = gunzip $compressed;
Uncompress bytes with IO::Compress::Gunzip.

  my $compressed = gzip $uncompressed;
Compress bytes with IO::Compress::Gzip.

  my $checksum = hmac_sha1_sum $bytes, 'passw0rd';
Generate HMAC-SHA1 checksum for bytes with Digest::SHA.
  # "11cedfd5ec11adc0ec234466d8a0f2a83736aa68"
  hmac_sha1_sum 'foo', 'passw0rd';

  my $str = html_attr_unescape $escaped;
Same as "html_unescape", but handles special rules from the HTML Living Standard <https://html.spec.whatwg.org> for HTML attributes.
  # "foo=bar&ltest=baz"
  html_attr_unescape 'foo=bar&ltest=baz';
  # "foo=bar<est=baz"
  html_attr_unescape 'foo=bar&lt;est=baz';

  my $str = html_unescape $escaped;
Unescape all HTML entities in string.
  # "<div>"
  html_unescape '&lt;div&gt;';

  my $checksum = md5_bytes $bytes;
Generate binary MD5 checksum for bytes with Digest::MD5.

  my $checksum = md5_sum $bytes;
Generate MD5 checksum for bytes with Digest::MD5.
  # "acbd18db4cc2f85cedef654fccc4a4d8"
  md5_sum 'foo';

  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 $str = punycode_decode $punycode;
Punycode decode string as described in RFC 3492 <http://tools.ietf.org/html/rfc3492>.
  # "bücher"
  punycode_decode 'bcher-kva';

  my $punycode = punycode_encode $str;
Punycode encode string as described in RFC 3492 <http://tools.ietf.org/html/rfc3492>.
  # "bcher-kva"
  punycode_encode 'bücher';

  my $quoted = quote $str;
Quote string.

  my $bool = secure_compare $str1, $str2;
Constant time comparison algorithm to prevent timing attacks.

  my $checksum = sha1_bytes $bytes;
Generate binary SHA1 checksum for bytes with Digest::SHA.

  my $checksum = sha1_sum $bytes;
Generate SHA1 checksum for bytes with Digest::SHA.
  # "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"
  sha1_sum 'foo';

  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.
  # "joel-is-a-slug"
  slugify 'Joel is a slug';
  # "this-is-my-resume"
  slugify 'This is: my - résumé! ☃ ';
  # "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 <http://tools.ietf.org/html/rfc6265>.

   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.
  # "one"
  split_header('one; two="three four", five=six')->[0][0];
  # "two"
  split_header('one; two="three four", five=six')->[0][2];
  # "three four"
  split_header('one; two="three four", five=six')->[0][3];
  # "five"
  split_header('one; two="three four", five=six')->[1][0];
  # "six"
  split_header('one; two="three four", five=six')->[1][1];

  my $time = 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.

  my $table = tablify [['foo', 'bar'], ['baz', 'yada']];
Row-oriented generator for text tables.
  # "foo   bar\nyada  yada\nbaz   yada\n"
  tablify [['foo', 'bar'], ['yada', 'yada'], ['baz', 'yada']];

  my $escaped = term_escape $str;
Escape all POSIX control characters except for "\n".
  # "foo\\x09bar\\x0d\n"
  term_escape "foo\tbar\r\n";

  my $trimmed = trim $str;
Trim whitespace characters from both ends of string.
  # "foo bar"
  trim '  foo bar  ';

  my $unindented = unindent $str;
Unindent multi-line string.
  # "foo\nbar\nbaz\n"
  unindent "  foo\n  bar\n  baz\n";

  my $str = unquote $quoted;
Unquote string.

  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 <http://tools.ietf.org/html/rfc3986>, the pattern used defaults to "^A-Za-z0-9\-._~".
  # "foo%3Bbar"
  url_escape 'foo;bar';

  my $str = url_unescape $escaped;
Decode percent encoded characters in string as described in RFC 3986 <http://tools.ietf.org/html/rfc3986>.
  # "foo;bar"
  url_unescape 'foo%3Bbar';

  my $escaped = xml_escape $str;
Escape unsafe characters "&", "<", ">", """ and "'" in string, but do not escape Mojo::ByteStream objects.
  # "&lt;div&gt;"
  xml_escape '<div>';
  # "<div>"
  use Mojo::ByteStream 'b';
  xml_escape b('<div>');

  my $encoded = xor_encode $str, $key;
XOR encode string with variable length key.

Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
2018-11-22 perl v5.28.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.