MouseX::Types(3pm)

πŸ“¦
version 0.06 Β· 6 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– MouseX::Types β€” the manual page, but make it cute πŸ’–

Okay bestie, listen. This is the manual for MouseX::Types (that's the MouseX::Types(3pm) page, yes ma'am), and it's literally just a way to organize your Mouse types into libraries. Like, instead of scattering type definitions everywhere like confetti at a gender reveal, you put them all in one tidy little library package. Iconic behavior. ✨


πŸ¦‹ SYNOPSIS πŸ¦‹

🌸 Library Definition 🌸

So first, you build the library.</purgle> Predeclare your own types, import the builtin ones, define subtypes, add coercions, done. Here's the whole vibe, verbatim:

package MyLibrary;

# predeclare our own types
use MouseX::Types
  -declare => [qw(
      PositiveInt NegativeInt
  )];

# import builtin types
use MouseX::Types::Mouse 'Int';

# type definition.
subtype PositiveInt,
    as Int,
    where { $_ > 0 },
    message { "Int is not larger than 0" };

subtype NegativeInt,
    as Int,
    where { $_ < 0 },
    message { "Int is not smaller than 0" };

# type coercion
coerce PositiveInt,
    from Int,
        via { 1 };

1;

Notice how subtype gets as, where, and message? And coerce gets from and via? That's the whole recipe, bestie. Slay. πŸ’…


πŸŽ€ Usage πŸŽ€

Now you actually use it. Pull the constants into your class and they're type names you can hand to isa. Also, free helper functions show up for checking and coercing:

package Foo;
use Mouse;
use MyLibrary qw( PositiveInt NegativeInt );

# use the exported constants as type names
has 'bar',
    isa    => PositiveInt,
    is     => 'rw';
has 'baz',
    isa    => NegativeInt,
    is     => 'rw';

sub quux {
    my ($self, $value);

    # test the value
    print "positive\n" if is_PositiveInt($value);
    print "negative\n" if is_NegativeInt($value);

    # coerce the value, NegativeInt doesn't have a coercion
    # helper, since it didn't define any coercions.
    $value = to_PositiveInt($value) or die "Cannot coerce";
}

1;

And yeah β€” notice the comment up there. NegativeInt doesn't get a to_NegativeInt helper because it never defined any coercions. Only the types with coercions get the to_ glow-up. Periodt. ✨

🫢 No cap: for each declared type you get an is_TypeName check helper, and a to_TypeName coercion helper only if that type actually declared coercions.

πŸ‘‘ AUTHORS πŸ‘‘

The legends behind this:

  • Kazuhiro Osawa β€” yappo <at> shibuya <dΓΆt> pl
  • Shawn M Moore
  • tokuhirom
  • Goro Fuji

…and, in their own words, with plenty of code borrowed from MooseX::Types. Respect where it's due. πŸ™‡β™€οΈ


🌈 REPOSITORY 🌈

Wanna clone it and poke around? Do that:

git clone git://github.com/yappo/p5-mousex-types.git MouseX-Types

⭐ SEE ALSO ⭐

  • Mouse 🐭
  • MooseX::Types 🦌

Legal stuff, but make it cute:

Copyright (c) 2008-2010, Kazuhiro Osawa and partly based on MooseX::Types, which is (c) Robert Sedlacek.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

🌸 Translation: it's Perl-licensed, so go forth and remix, bestie.

And that's the whole page, fr fr. This gem is from perl v5.34.0, dated 2022-06-16 β€” the MouseX::Types(3pm) manual, and honestly? Ate. πŸ’– πŸ¦‹ πŸŽ€ ✨

πŸ“¦ where this page comes from