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

Defaults::Modern - Yet another approach to modernistic Perl

SYNOPSIS

use Defaults::Modern;

# Function::Parameters + List::Objects::WithUtils + types ->
fun to_immutable ( (ArrayRef | ArrayObj) $arr ) {
  # blessed() and confess() are available (amongst others):
  my $immutable = immarray( blessed $arr ? $arr->all : @$arr );
  confess 'No items in array!' unless $immutable->has_any;
  $immutable
}

package My::Foo {
  use Defaults::Modern;

  # define keyword for defining constants ->
  define ARRAY_MAX = 10;

  # Moo(se) with types ->
  use Moo;

  has myarray => (
    is      => 'ro',
    isa     => ArrayObj,
    writer  => '_set_myarray',
    coerce  => 1,
    builder => sub { [] },
  );

  # Method with optional positional param and implicit $self ->
  method slice_to_max (Int $max = -1) {
    my $arr = $self->myarray;
    $self->_set_myarray( 
      $arr->sliced( 0 .. $max >= 0 ? $max : ARRAY_MAX )
    )
  }
}

# Optionally autobox list-type refs via List::Objects::WithUtils ->
use Defaults::Modern 'autobox_lists';
my $obj = +{ foo => 'bar', baz => 'quux' }->inflate;
my $baz = $obj->baz;

# See DESCRIPTION for complete details on imported functionality.

DESCRIPTION

Yet another approach to writing Perl in a modern style.

. . . also saves me extensive typing ;-)

When you use Defaults::Modern, you get:

If you import the tag autobox_lists, ARRAY and HASH type references are autoboxed via List::Objects::WithUtils:

use Defaults::Modern 'autobox_lists';
my $itr = [ 1 .. 10 ]->natatime(2);

Moo version 2+ is depended upon in order to guarantee availability, but not automatically imported:

use Defaults::Modern;
use Moo;
use MooX::TypeTiny;   # recommended for faster inline type checks

has foo => (
  is  => 'ro',
  isa => ArrayObj,
  coerce  => 1,
  default => sub { [] },
);

If you're building classes, you may want to look into namespace::clean / namespace::sweep or similar -- Defaults::Modern imports an awful lot of Stuff:

use Defaults::Modern;
use Moo;
use namespace::clean;
# ...

SEE ALSO

This package just glues together useful parts of CPAN, the most visible portions of which come from the following modules:

Carp

Function::Parameters

List::Objects::WithUtils and List::Objects::Types

match::simple

Path::Tiny

PerlX::Maybe

Quote::Code

Scalar::Util

Switch::Plain

Try::Tiny

Types::Standard

Type::Tiny

AUTHOR

Jon Portnoy <avenj@cobaltirc.org>

Licensed under the same terms as Perl.

Inspired by Defaults::Mauke and Moops.

The code backing the define keyword is forked from TOBYINK's PerlX::Define to avoid the Moops dependency and is copyright Toby Inkster.