Type::Library(3)

πŸ’– Type::Library β€” the manual, but make it cute πŸ’–

okay so this is Type::Library(3), from the User Contributed Perl Documentation section of the manual. bestie, we're about to make type libraries and it's gonna be so good.


✨ NAME ✨

Type::Library β€” tiny, yet Moo(se)-compatible type libraries. it's giving minimal effort, maximum slay. πŸ’…


🌸 SYNOPSIS 🌸

here's the whole vibe, copy-paste ready, no cap:

package Types::Mine {
   use Scalar::Util qw(looks_like_number);
   use Type::Library -base;
   use Type::Tiny;

   my $NUM = "Type::Tiny"->new(
      name       => "Number",
      constraint => sub { looks_like_number($_) },
      message    => sub { "$_ ain't a number" },
   );

   __PACKAGE__->meta->add_type($NUM);

   __PACKAGE__->meta->make_immutable;
}

package Ermintrude {
   use Moo;
   use Types::Mine qw(Number);
   has favourite_number => (is => "ro", isa => Number);
}

package Bullwinkle {
   use Moose;
   use Types::Mine qw(Number);
   has favourite_number => (is => "ro", isa => Number);
}

package Maisy {
   use Mouse;
   use Types::Mine qw(Number);
   has favourite_number => (is => "ro", isa => Number);
}

look at that. one type library, three whole object systems, zero drama. πŸ¦‹


πŸ‘‘ STATUS πŸ‘‘

this module is covered by the Type-Tiny stability policy. periodt.


πŸ’œ DESCRIPTION πŸ’œ

Type::Library is a tiny class for creating MooseX::Types-like type libraries which are compatible with Moo, Moose and Mouse. the way it works with all three of them? iconic. ✨

ngl, if you're reading this because you want to create a type library, you're probably better off reading Type::Tiny::Manual::Libraries instead. that one's the tutorial, this one's the reference. iykyk. πŸ“š

πŸŽ€ Type library methods πŸŽ€

a type library is a singleton class. use the meta method to get a blessed object which other methods can get called on. like, for example:

Types::Mine->meta->add_type($foo);
  • add_type($type) or add_type(%opts) β€” add a type to the library. if %opts is given, then this method calls "Type::Tiny->new(%opts)" first, and adds the resultant type.

okay bestie this part is important: adding a type named Foo to the library will automatically define four functions in the library's namespace:

  • "Foo" β€” returns the Type::Tiny object.
  • is_Foo($value) β€” returns true iff $value passes the type constraint.
  • assert_Foo($value) β€” returns $value iff $value passes the type constraint. dies otherwise. πŸ’€
  • to_Foo($value) β€” coerces the value to the type.
  • get_type($name) β€” gets the "Type::Tiny" object corresponding to the name.
  • has_type($name) β€” boolean; returns true if the type exists in the library.
  • "type_names" β€” list all types defined by the library.
  • add_coercion($c) or add_coercion(%opts) β€” add a standalone coercion to the library. if %opts is given, then this method calls "Type::Coercion->new(%opts)" first, and adds the resultant coercion.

and same energy as before: adding a coercion named FooFromBar to the library will automatically define a function in the library's namespace:

  • "FooFromBar" β€” returns the Type::Coercion object.
  • get_coercion($name) β€” gets the "Type::Coercion" object corresponding to the name.
  • has_coercion($name) β€” boolean; returns true if the coercion exists in the library.
  • "coercion_names" β€” list all standalone coercions defined by the library.
  • import(@args) β€” Type::Library-based libraries are exporters. yes, they hand things out. generous queens. 🫢
  • "make_immutable" β€” prevents new type constraints and coercions from being added to the library, and also calls "$type->coercion->freeze" on every type constraint in the library.
πŸ•°οΈ a lil history moment, no cap: prior to Type::Library v2, "make_immutable" would call "$type->coercion->freeze" on every constraint in the library, but not prevent new type constraints and coercions from being added to the library.

🧚 Type library exported functions 🧚

type libraries are exporters. for the purposes of the following examples, assume that the "Types::Mine" library defines types Number and String. every single one of these is a real move you can pull:

# Exports nothing.
#
use Types::Mine;

# Exports a function "String" which is a constant returning
# the String type constraint.
#
use Types::Mine qw( String );

# Exports both String and Number as above.
#
use Types::Mine qw( String Number );

# Same.
#
use Types::Mine qw( :types );

# Exports "coerce_String" and "coerce_Number", as well as any other
# coercions
#
use Types::Mine qw( :coercions );

# Exports a sub "is_String" so that "is_String($foo)" is equivalent
# to "String->check($foo)".
#
use Types::Mine qw( is_String );

# Exports "is_String" and "is_Number".
#
use Types::Mine qw( :is );

# Exports a sub "assert_String" so that "assert_String($foo)" is
# equivalent to "String->assert_return($foo)".
#
use Types::Mine qw( assert_String );

# Exports "assert_String" and "assert_Number".
#
use Types::Mine qw( :assert );

# Exports a sub "to_String" so that "to_String($foo)" is equivalent
# to "String->coerce($foo)".
#
use Types::Mine qw( to_String );

# Exports "to_String" and "to_Number".
#
use Types::Mine qw( :to );

# Exports "String", "is_String", "assert_String" and "coerce_String".
#
use Types::Mine qw( +String );

# Exports everything.
#
use Types::Mine qw( :all );

type libraries automatically inherit from Exporter::Tiny; see the documentation of that module for tips and tricks importing from libraries. go read it, it's a whole thing. ✨

πŸ¦‹ Type::Library's methods πŸ¦‹

the above sections describe the characteristics of libraries built with Type::Library. the following methods are available on Type::Library itself. main character energy only.

  • "setup_type_library( $package, $utils, \@extends )" β€” sets up a package to be a type library. $utils is a boolean indicating whether to import Type::Utils into the package. @extends is a list of existing type libraries the package should extend.

πŸ› BUGS πŸ›

please report any bugs to <https://github.com/tobyink/p5-type-tiny/issues>. (that's the link, bestie, go click it if something's acting up πŸ™ˆ)


🌈 SEE ALSO 🌈

  • Type::Tiny::Manual.
  • Type::Tiny, Type::Utils, Types::Standard, Type::Coercion.
  • Moose::Util::TypeConstraints, Mouse::Util::TypeConstraints.

πŸ’– AUTHOR πŸ’–

Toby Inkster <tobyink@cpan.org>. πŸ‘‘


this software is copyright (c) 2013-2014, 2017-2025 by Toby Inkster.

this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. slay. πŸ’…


πŸ’€ DISCLAIMER OF WARRANTIES πŸ’€

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

(translation: no promises, bestie, it's giving "as is" fr fr 😭)


and that's the whole page!! this one came from perl v5.42.0, dated 2026-01-17, filed under Type::Library(3). mwah, love u, go write some type libraries πŸ’œβœ¨πŸŒΈ

πŸ“¦ where this page comes from