Ubuntu Manpages

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.

  my $bytes = b64_decode $b64;

Base64 decode bytes.

  my $b64 = b64_encode $bytes;
  my $b64 = b64_encode $bytes, "\n";

Base64 encode bytes, 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.

  # "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 and 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.

  my $line = get_line \$str;

Extract whole line from string or return "undef". Lines are expected to end with "0x0d 0x0a" or 0x0a.

  my $checksum = hmac_sha1_sum $bytes, 'passw0rd';

Generate HMAC-SHA1 checksum for bytes.

  my $str = html_unescape $escaped;

Unescape all HTML entities in string.

  my $checksum = md5_bytes $bytes;

Generate binary MD5 checksum for bytes.

  my $checksum = md5_sum $bytes;

Generate MD5 checksum for bytes.

  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.

  my $punycode = punycode_encode $str;

Punycode encode string.

  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.

  my $checksum = sha1_sum $bytes;

Generate SHA1 checksum for bytes.

  my $bytes = slurp '/etc/passwd';

Read all data at once from file.

   my $tree = split_header 'foo="bar baz"; test=123, yada';

Split HTTP header value.

  # "one"
  split_header('one; two="three four", five=six')->[0][0];
  # "three four"
  split_header('one; two="three four", five=six')->[0][3];
  # "five"
  split_header('one; two="three four", five=six')->[1][0];

  $bytes = spurt $bytes, '/etc/passwd';

Write all data at once to file.

  my $squished = squish $str;

Trim whitespace characters from both ends of string and then change all consecutive groups of whitespace into one space each.

  my $time = steady_time;

High resolution time, resilient to time jumps if a monotonic clock is available through Time::HiRes.

  my $trimmed = trim $str;

Trim whitespace characters from both ends of string.

  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, the pattern used defaults to "^A-Za-z0-9\-._~".

  my $str = url_unescape $escaped;

Decode percent encoded characters in string.

  my $escaped = xml_escape $str;

Escape unsafe characters "&", "<", ">", """ and "'" in string.

  my $encoded = xor_encode $str, $key;

XOR encode string with variable length key.

Mojolicious, Mojolicious::Guides, <http://mojolicio.us>.