NAME

Mojar::Util - General utility functions

SYNOPSIS

use Mojar::Util 'transcribe';

my $replaced = transcribe $original, '_' => '-', '-' => '_';

DESCRIPTION

Miscellaneous utility functions.

FUNCTIONS

detitlecase

my $snakecase = detitlecase $titlecase;
my $snakecase = detitlecase $titlecase => $separator;

Convert title-case string to hyphenated lowercase.

# "foo-bar"
detitlecase 'FooBar' => '-';

# "foo-bar::baz"
detitlecase 'FooBar::Baz' => '-';

# "i_foo_bar"
detitlecase 'iFooBar';

titlecase

my $titlecase = titlecase $snakecase;
my $titlecase = titlecase $snakecase => $separator;
my $titlecase = titlecase $snakecase => $separator, $do_camelcase;

Convert snake-case string to hyphenated lowercase, with optional additional translations.

# "FooBar"
titlecase 'foo_bar';

# "FooBar"
titlecase 'foo-bar' => '-';

# "fooBar"
titlecase 'foo_bar' => undef, 1;

# "FooBar_Baz"
titlecase 'foo-bar_baz' => '-';

# 'iFooBar';
titlecase i_foo_bar => undef, 1;

transcribe

my $template_base = transcribe $url_path, '/' => '_';

my $controller_class =
    transcribe $url_path, '/' => '::', sub { titlecase $_[0] => '-' };

my $with_separators_swapped = transcribe $string, '_' => '-', '-' => '_';

spurt

my $written_string = spurt $path, @content;

spurt '/tmp/test.txt', "Some\ntext\n";
spurt '/tmp/test.txt', "More\n", "text\n";  # overwrites previous content

Similar to Mojo::Util::spurt but with opposite argument order and accepting list of content. If passed a list, it joins the parts together before writing.

->syswrite(join '', @content)

dumper

say dumper $object;
print dumper($object), "\n";
$log->debug(dumper $hashref, $arrayref, $string, $numeric);

Based on Data::Dumper it is simply a tidied (post-processed) version. It is argument-greedy and if passed more than one argument will wrap them in an arrayref and then later strip away that dummy layer. In the resulting string, "TOP" refers to the top-most (single, possibly invisible) entity.

hash_or_hashref

my $hashref = hash_or_hashref({ A => 1, B => 2 });
my $hashref = hash_or_hashref($object);
my $hashref = hash_or_hashref(A => 1, B => 2);
my $hashref = hash_or_hashref();

Takes care of those cases where you want to handle hashes or hashrefs. Always gives a hashref if it can, otherwise dies.

RATIONALE

Mojo::Util is packed with useful functions, but I kept hitting occasions when I needed to transcribe characters in the result or apply my own de/titlecase. With the functions here I get to use Mojo::Util more widely but I also find these useful independently.

SEE ALSO

Mojo::Util, String::Util, Data::Dump.