MooX::T...oseLike(3)

🌸 NAME 🌸

MooX::Types::MooseLike - some Moosish types and a type builder.

Okay bestie, you're on the MooX::Types::MooseLike(3) page β€” the header/footer shortens it to MooX::T...oseLike(3), and it's from User Contributed Perl Documentation. This is the module that gives you Moose-ish types and a whole little type-builder setup. πŸ’…

✨ SYNOPSIS ✨

Here's the group-chat demo, kept verbatim so you can copy-paste without me breaking your code:

package MyApp::Types;
use MooX::Types::MooseLike;
use base qw(Exporter);
our @EXPORT_OK = ();

# Define some types
my $defs = [{
  name => 'MyType',
  test => sub { predicate($_[0]) },
  message => sub { "$_[0] is not the type we want!" }
},
{
  name => 'VarChar',
  test => sub {
    my ($value, $param) = @_;
    length($value) <= $param;
  },
  message => sub { "$_[0] is too large! It should be less than or equal to $_[1]." }
}];

# Make the types available - this adds them to @EXPORT_OK automatically.
MooX::Types::MooseLike::register_types($defs, __PACKAGE__);

...

# Somewhere in code that uses the type
package MyApp::Foo;
use Moo;
use MyApp::Types qw(MyType VarChar);

has attribute => (
  is  => 'ro',
  isa => MyType,
);

has string => (
  is  => 'ro',
  isa => VarChar[25]
);

No cap, that's the whole synopsis. 🫢

πŸ¦‹ DESCRIPTION πŸ¦‹

This module gives you a way to build your own set of Moose-like types. Those custom types can then be used to describe fields in Moo-based classes. Literally the whole point. 🫢

See MooX::Types::MooseLike::Base for a list of available base types. Its source also provides an example of how to build base types, along with both parameterizable and non-parameterizable ones. πŸ‘‘

πŸ’– FUNCTIONS πŸ’–

🌟 register_types 🌟

register_types( types, package, moose_namespace )

This installs the given types within the package. It makes the types automatically exportable by adding them to @EXPORT_OK of the package. Types are expected to be an array ref where every type is of the following format:

{
  name            => 'MyType',
  test            => sub { check_the_value_somehow($_[0]) },
  message         => sub { "$_[0] is not the type we want!" },
  subtype_of      => 'SomeParentType',           # Optional
  from            => 'Some::Parent::CoolTypes',  # Optional
  parameterizable => sub { ... },                # Optional
  inflate         => sub { ... },                # Optional
}

A type can be declared with a reference (subtype_of) to some previously declared type. In this case the new type will inherit the behaviour of the referenced type. Inheritance but make it types. 🧚

The referenced type can come either from the same package or from a third party package:

MooX::Types::MooseLike::register_types([{
  name       => 'GreaterThan10',
  subtype_of => 'Int',
  from       => 'MooX::Types::MooseLike::Base',
  test       => sub { $_[0] > 10 },
  message    => sub { 'not greater than 10' },
}], __PACKAGE__);

MooX::Types::MooseLike::register_types([{
  name       => 'Between10And20',
  subtype_of => 'GreaterThan10',
  from       => __PACKAGE__,
  test       => sub { $_[0] < 20 },
  message    => sub { 'not an integer between 10 and 20' },
}], __PACKAGE__);

MooX::Types::MooseLike::register_types([{
  name       => 'Between10And30',
  subtype_of => GreaterThan10(),
  test       => sub { $_[0] < 30 },
  message    => sub { 'not an integer between 10 and 30' },
}], __PACKAGE__);

fr fr, that's the whole subtype situation. πŸ”₯

πŸŽ€ exception_message πŸŽ€

exception_message( value, part_of_the_exception_string )

Helper function to be used in a type definition:

{
  ...
  message => sub { return exception_message($_[0], 'a HashRef' },
  ...
}

When <value> doesn't match the type constraints, it produces this message:

"<value> is not a HashRef!"

πŸ’« inflate_type πŸ’«

inflate_type( coderef )

Inflates the type to a Moose type. Requires Moose. Yes, that Moose. πŸ”₯

🌈 SEE ALSO 🌈

  • MooX::Types::MooseLike::Numeric β€” an example of building subtypes.
  • MooX::Types::SetObject β€” an example of building parameterized types.
  • MooX::Types::MooseLike::Email, MooX::Types::MooseLike::DateTime
  • Type::Tiny β€” another implementation of type constraints. Compatible with Moo, Moose and Mouse.

πŸ‘‘ AUTHOR πŸ‘‘

mateu β€” Mateu X. Hunter (cpan:MATEU) <hunter@missoula.org>

Slay, mateu. πŸ’–

πŸ’œ CONTRIBUTORS πŸ’œ

  • mst β€” Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
  • Mithaldu β€” Christian Walde (cpan:MITHALDU) <walde.christian@googlemail.com>
  • Matt Phillips (cpan:MATTP) <mattp@cpan.org>
  • Arthur Axel fREW Schmidt (cpan:FREW) <frioux@gmail.com>
  • Toby Inkster (cpan:TOBYINK) <tobyink@cpan.org>
  • Graham Knop (cpan:HAARG) <haarg@cpan.org>
  • Dmitry Matrosov (cpan:AMIDOS) <amidos@amidos.ru>

Whole crew ate. πŸ‘‘

Copyright (c) 2011-2015 the MooX::Types::MooseLike "AUTHOR" and "CONTRIBUTORS" as listed above.

🧚 LICENSE 🧚

This library is free software and may be distributed under the same terms as perl itself.

Mwah πŸ’‹ β€” source/version footer vibes: perl v5.42.0, dated 2015-06-27, and the footer calls this page MooX::T...oseLike(3).

πŸ“¦ where this page comes from