The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Type::Library - tiny, yet Moo(se)-compatible type libraries

SYNOPSIS

   package MyTypes {
      use Scalar::Util qw(looks_like_number);
      use base "Type::Library";
      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 Ermintrude {
      use Moo;
      use MyTypes qw(Number);
      has favourite_number => (is => "ro", isa => Number);
   }
   
   # Note the "-moose" flag when importing!
   package Bullwinkle {
      use Moose;
      use MyTypes -moose, qw(Number);
      has favourite_number => (is => "ro", isa => Number);
   }
   
   # Note the "-mouse" flag when importing!
   package Maisy {
      use Mouse;
      use MyTypes -mouse, qw(Number);
      has favourite_number => (is => "ro", isa => Number);
   }

DESCRIPTION

Type::Library is a tiny class for creating MooseX::Types-like type libraries which are compatible with Moo and Moose.

If you're reading this because you want to create a type library, then you're probably better off reading Type::Tiny::Intro.

Methods

A type library is a singleton class. Use the meta method to get a blessed object which other methods can get called on. For example:

   MyTypes->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.

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 contraint.

assert_Foo($value)

Returns true iff $value passes the type contraint. Dies otherwise.

to_Foo($value)

Coerces the value to the type. (Not implemented yet.)

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.

import(@args)

Type::Library-based libraries are exporters.

Export

Type libraries are exporters. For the purposes of the following examples, assume that the MyTypes library defines types Number and String.

   # Exports nothing.
   # 
   use MyTypes;
   
   # Exports a function "String" which is a constant returning
   # the String type constraint.
   #
   use MyTypes qw( String );
   
   # Exports both String and Number as above.
   #
   use MyTypes qw( String Number );
   
   # Same.
   #
   use MyTypes qw( :types );
   
   # Exports a sub "is_String" so that "is_String($foo)" is equivalent
   # to "String->check($foo)".
   #
   use MyTypes qw( is_String );
   
   # Exports "is_String" and "is_Number".
   #
   use MyTypes qw( :is );
   
   # Exports a sub "assert_String" so that "assert_String($foo)" is
   # equivalent to "String->assert_valid($foo)".
   #
   use MyTypes qw( assert_String );
   
   # Exports "assert_String" and "assert_Number".
   #
   use MyTypes qw( :assert );
   
   # Exports a sub "to_String" so that "to_String($foo)" is equivalent
   # to "String->coerce($foo)".
   #
   use MyTypes qw( to_String );
   
   # Exports "to_String" and "to_Number".
   #
   use MyTypes qw( :to );
   
   # Exports "String", "is_String", "assert_String" and "coerce_String".
   #
   use MyTypes qw( +String );
   
   # Exports everything.
   #
   use MyTypes qw( :all );

Adding -mouse or -moose to the export list ensures that all the type constraints exported are Mouse or Moose compatible respectively.

BUGS

Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=Type-Tiny.

SEE ALSO

Type::Tiny::Manual.

Type::Tiny, Type::Utils, Type::Standard, Type::Coercion.

Moose::Util::TypeConstraints, Mouse::Util::TypeConstraints.

AUTHOR

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE

This software is copyright (c) 2013 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.

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.