The types provided with Moose are by design global. This package helps you to organise and selectively import your own and the built-in types in libraries. As a nice side effect, it catches typos at compile-time too.

However, the main reason for this module is to provide an easy way to not have conflicts with your type names, since the internal fully qualified names of the types will be prefixed with the library's name.

This module will also provide you with some helper functions to make it easier to use Moose types in your code.

String type names will produce a warning, unless it's for a class_type or role_type declared within the library, or a fully qualified name like 'MyTypeLibrary::Foo'.

A handler that will take a value and coerce it into the $type. It will return a false value if the type could not be coerced.

Important Note: This handler will only be exported for types that can do type coercion. This has the advantage that a coercion to a type that cannot hasn't defined any coercions will lead to a compile-time error.

A MooseX::Types is just a normal Perl module. Unlike Moose itself, it does not install use strict and use warnings in your class by default, so this is up to you.

The only thing a library is required to do is

use MooseX::Types -declare => \@types;

with @types being a list of types you wish to define in this library. This line will install a proper base class in your package as well as the full set of handlers for your declared types. It will then hand control over to Moose::Util::TypeConstraints' import method to export the functions you will need to declare your types.

If you want to use Moose' built-in types (e.g. for subtyping) you will want to

use MooseX::Types::Moose @types;

to import the helpers from the shipped MooseX::Types::Moose library which can export all types that come with Moose.

You will have to define coercions for your types or your library won't export a "to_$type" coercion helper for it.

Note that you currently cannot define types containing ::, since exporting would be a problem.

You also don't need to use warnings and strict, since the definition of a library automatically exports those.

A library makes the types quasi-unique by prefixing their names with (by default) the library package name. If you're only using the type handler functions provided by MooseX::Types, you shouldn't ever have to use a type's actual full name.

The Perlop manpage has this to say about the '=>' operator: "The => operator is a synonym for the comma, but forces any word (consisting entirely of word characters) to its left to be interpreted as a string (as of 5.001). This includes words that might otherwise be considered a constant or function call."

Due to this stringification, the following will NOT work as you might think:

subtype StrOrArrayRef => as Str|ArrayRef;

The 'StrOrArrayRef' will have it's stringification activated this causes the subtype to not be created. Since the bareword type constraints are not strings you really should not try to treat them that way. You will have to use the ',' operator instead. The author's of this package realize that all the Moose documention and examples nearly uniformly use the '=>' version of the comma operator and this could be an issue if you are converting code.

This is a workaround and I am exploring how to make these modules work better together. I realize this workaround will lead a lot of duplication in your export declarations and will be onerous for large type libraries. Patches and detailed test cases welcome. See the tests directory for a start on this.