The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mojar::Util - General utility functions

SYNOPSIS

  use Mojar::Util 'transcribe';

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

DESCRIPTION

Miscellaneous utility functions.

FUNCTIONS

as_bool

  $boolean = as_bool($val);

Convert arbitrary scalar to a Boolean, intended to accommodate strings equivalent to on/off, true/false, yes/no. The following are true.

  as_bool('ON'), as_bool('true'), as_bool(42), as_bool('Yes'), as_bool('NO!')

The following are false.

  as_bool('off'), as_bool('False'), as_bool(0), as_bool('NO'), as_bool(undef)

been_numeric

  $probably_a_number = been_numeric($val);

Introspects a flag indicating whether the value has been treated as a number. cf: Scalar::Util::looks_like_number.

snakecase

  $snakecase = snakecase $titlecase;
  $snakecase = snakecase $titlecase => $separator;

Convert title-case string to snakecase. Also converts from camelcase.

  snakecase 'iFooBar';
  # "i_foo_bar"

Rather than using an underscore, a different separator can be specified.

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

  snakecase 'FFooBar/BazZoo' => '-';
  # "f-foo-bar/baz-zoo"

unsnakecase

  $titlecase = unsnakecase $snakecase;
  $titlecase = unsnakecase $snakecase => $separator;
  $titlecase = unsnakecase $snakecase => $separator, $want_camelcase;

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

  unsnakecase 'foo_bar';
  # "FooBar"

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

An undefined separator defaults to underscore, which is useful when you only want to specify a camelcase result.

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

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

There is only one level of separator; for more see transcribe.

  unsnakecase 'foo-bar_baz' => '-';
  # "FooBar_baz"

Leading separators pass through.

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

As do trailing separators.

  unsnakecase '__bar_baz__';
  # "__BarBaz__"

transcribe

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

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

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

Repeatedly replaces a character/string with another character/string. Can even swap between values, as shown in that last example.

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.

This is intended to be clear and succinct to support error messages and debug statements. It is not suitable for serialising entities because it does not try to maintain round trip stability. (ie Don't try to evaluate its output.)

hash_or_hashref

  $hashref = hash_or_hashref({ A => 1, B => 2 });
  $hashref = hash_or_hashref($object);  # $object if hashref-based
  $hashref = hash_or_hashref(A => 1, B => 2);
  $hashref = hash_or_hashref();  # {}

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

check_exists

  sub something {
    my ($self, %param) = @_;
    check_exists [qw(dbh log)], %param;

  sub something2 {
    my $self = shift;
    my ($dbh, $log) = check_exists [qw(dbh log)], @_;

  sub something3 {
    my ($self, $param) = @_;
    my ($dbh) = check_exists 'dbh', $param;

  package MyClass;
  use Mojar::Util ();
  sub exists = {
    my $keys = ref $_[0] eq 'ARRAY' ? $_[0] : [ @_ ];
    return Mojar::Util::exists($keys, $self);
  }

Checks that required parameters have been passed. Takes a string or arrayref of strings that are required keys, and a hash or hashref of parameters. Throws an exception if one or more strings do not exist as keys in the parameters; otherwise returns the list of parameter values.

SEE ALSO

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