Nic Sandfield > Mojar-1.062 > Mojar::Util

Download:
Mojar-1.062.tar.gz

Dependencies

Annotate this POD

Website

View/Report Bugs
Source   Latest Release: Mojar-2.001

NAME ^

Mojar::Util - General utility functions

SYNOPSIS ^

  use Mojar::Util 'transcribe';

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

DESCRIPTION ^

Miscellaneous utility functions.

FUNCTIONS ^

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.

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.

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.

syntax highlighting: