.\" Automatically generated by Pod::Man 4.07 (Pod::Simple 3.32) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "MooseX::Types 3pm" .TH MooseX::Types 3pm "2017-06-17" "perl v5.24.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" MooseX::Types \- Organise your Moose types in libraries .SH "VERSION" .IX Header "VERSION" version 0.50 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .SS "Library Definition" .IX Subsection "Library Definition" .Vb 1 \& package MyLibrary; \& \& # predeclare our own types \& use MooseX::Types \-declare => [ \& qw( \& PositiveInt \& NegativeInt \& ArrayRefOfPositiveInt \& ArrayRefOfAtLeastThreeNegativeInts \& LotsOfInnerConstraints \& StrOrArrayRef \& MyDateTime \& ) \& ]; \& \& # import builtin types \& use MooseX::Types::Moose qw/Int HashRef/; \& \& # 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 }; \& \& # with parameterized constraints. \& \& subtype ArrayRefOfPositiveInt, \& as ArrayRef[PositiveInt]; \& \& subtype ArrayRefOfAtLeastThreeNegativeInts, \& as ArrayRef[NegativeInt], \& where { scalar(@$_) > 2 }; \& \& subtype LotsOfInnerConstraints, \& as ArrayRef[ArrayRef[HashRef[Int]]]; \& \& # with TypeConstraint Unions \& \& subtype StrOrArrayRef, \& as Str|ArrayRef; \& \& # class types \& \& class_type \*(AqDateTime\*(Aq; \& \& # or better \& \& class_type MyDateTime, { class => \*(AqDateTime\*(Aq }; \& \& coerce MyDateTime, \& from HashRef, \& via { DateTime\->new(%$_) }; \& \& 1; .Ve .SS "Usage" .IX Subsection "Usage" .Vb 3 \& package Foo; \& use Moose; \& use MyLibrary qw( PositiveInt NegativeInt ); \& \& # use the exported constants as type names \& has \*(Aqbar\*(Aq, \& isa => PositiveInt, \& is => \*(Aqrw\*(Aq; \& has \*(Aqbaz\*(Aq, \& isa => NegativeInt, \& is => \*(Aqrw\*(Aq; \& \& sub quux { \& my ($self, $value); \& \& # test the value \& print "positive\en" if is_PositiveInt($value); \& print "negative\en" if is_NegativeInt($value); \& \& # coerce the value, NegativeInt doesn\*(Aqt have a coercion \& # helper, since it didn\*(Aqt define any coercions. \& $value = to_PositiveInt($value) or die "Cannot coerce"; \& } \& \& 1; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" The type system provided by Moose effectively makes all of its builtin type global, as are any types you declare with Moose. This means that every module that declares a type named \f(CW\*(C`PositiveInt\*(C'\fR is sharing the same type object. This can be a problem when different parts of the code base want to use the same name for different things. .PP This package lets you declare types using short names, but behind the scenes it namespaces all your type declarations, effectively prevent name clashes between packages. .PP This is done by creating a type library module like \f(CW\*(C`MyApp::Types\*(C'\fR and then importing types from that module into other modules. .PP As a side effect, the declaration mechanism allows you to write type names as barewords (really function calls), which catches typos in names at compile time rather than run time. .PP This module also provides some helper functions for using Moose types outside of attribute declarations. .PP If you mix string-based names with types created by this module, it will warn, with a few exceptions. If you are declaring a \f(CW\*(C`class_type()\*(C'\fR or \&\f(CW\*(C`role_type()\*(C'\fR within your type library, or if you use a fully qualified name like \f(CW"MyApp::Foo"\fR. .SH "LIBRARY DEFINITION" .IX Header "LIBRARY DEFINITION" A MooseX::Types is just a normal Perl module. Unlike Moose itself, it does not install \f(CW\*(C`use strict\*(C'\fR and \f(CW\*(C`use warnings\*(C'\fR in your class by default, so this is up to you. .PP The only thing a library is required to do is .PP .Vb 1 \& use MooseX::Types \-declare => \e@types; .Ve .PP with \f(CW@types\fR 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' \&\f(CW\*(C`import\*(C'\fR method to export the functions you will need to declare your types. .PP If you want to use Moose' built-in types (e.g. for subtyping) you will want to .PP .Vb 1 \& use MooseX::Types::Moose @types; .Ve .PP to import the helpers from the shipped MooseX::Types::Moose library which can export all types that come with Moose. .PP You will have to define coercions for your types or your library won't export a \*(L"to_$type\*(R" coercion helper for it. .PP Note that you currently cannot define types containing \f(CW\*(C`::\*(C'\fR, since exporting would be a problem. .PP You also don't need to use \f(CW\*(C`warnings\*(C'\fR and \f(CW\*(C`strict\*(C'\fR, since the definition of a library automatically exports those. .SH "LIBRARY USAGE" .IX Header "LIBRARY USAGE" You can import the \*(L"type helpers\*(R" of a library by \f(CW\*(C`use\*(C'\fRing it with a list of types to import as arguments. If you want all of them, use the \f(CW\*(C`:all\*(C'\fR tag. For example: .PP .Vb 2 \& use MyLibrary \*(Aq:all\*(Aq; \& use MyOtherLibrary qw( TypeA TypeB ); .Ve .PP MooseX::Types comes with a library of Moose' built-in types called MooseX::Types::Moose. .PP The exporting mechanism is, since version 0.5, implemented via a wrapper around Sub::Exporter. This means you can do something like this: .PP .Vb 2 \& use MyLibrary TypeA => { \-as => \*(AqMyTypeA\*(Aq }, \& TypeB => { \-as => \*(AqMyTypeB\*(Aq }; .Ve .SH "TYPE HANDLER FUNCTIONS" .IX Header "TYPE HANDLER FUNCTIONS" .ie n .SS "$type" .el .SS "\f(CW$type\fP" .IX Subsection "$type" A constant with the name of your type. It contains the type's fully qualified name. Takes no value, as all constants. .SS "is_$type" .IX Subsection "is_$type" This handler takes a value and tests if it is a valid value for this \&\f(CW$type\fR. It will return true or false. .SS "to_$type" .IX Subsection "to_$type" A handler that will take a value and coerce it into the \f(CW$type\fR. It will return a false value if the type could not be coerced. .PP \&\fBImportant Note\fR: This handler will only be exported for types that can do type coercion. This has the advantage that a coercion to a type that has not defined any coercions will lead to a compile-time error. .SH "WRAPPING A LIBRARY" .IX Header "WRAPPING A LIBRARY" You can define your own wrapper subclasses to manipulate the behaviour of a set of library exports. Here is an example: .PP .Vb 4 \& package MyWrapper; \& use strict; \& use MRO::Compat; \& use base \*(AqMooseX::Types::Wrapper\*(Aq; \& \& sub coercion_export_generator { \& my $class = shift; \& my $code = $class\->next::method(@_); \& return sub { \& my $value = $code\->(@_); \& warn "Coercion returned undef!" \& unless defined $value; \& return $value; \& }; \& } \& \& 1; .Ve .PP This class wraps the coercion generator (e.g., \f(CW\*(C`to_Int()\*(C'\fR) and warns if a coercion returned an undefined value. You can wrap any library with this: .PP .Vb 4 \& package Foo; \& use strict; \& use MyWrapper MyLibrary => [qw( Foo Bar )], \& Moose => [qw( Str Int )]; \& \& ... \& 1; .Ve .PP The \f(CW\*(C`Moose\*(C'\fR library name is a special shortcut for MooseX::Types::Moose. .SS "Generator methods you can overload" .IX Subsection "Generator methods you can overload" .ie n .IP "type_export_generator( $short, $full )" 4 .el .IP "type_export_generator( \f(CW$short\fR, \f(CW$full\fR )" 4 .IX Item "type_export_generator( $short, $full )" Creates a closure returning the type's Moose::Meta::TypeConstraint object. .ie n .IP "check_export_generator( $short, $full, $undef_message )" 4 .el .IP "check_export_generator( \f(CW$short\fR, \f(CW$full\fR, \f(CW$undef_message\fR )" 4 .IX Item "check_export_generator( $short, $full, $undef_message )" This creates the closure used to test if a value is valid for this type. .ie n .IP "coercion_export_generator( $short, $full, $undef_message )" 4 .el .IP "coercion_export_generator( \f(CW$short\fR, \f(CW$full\fR, \f(CW$undef_message\fR )" 4 .IX Item "coercion_export_generator( $short, $full, $undef_message )" This is the closure that's doing coercions. .SS "Provided Parameters" .IX Subsection "Provided Parameters" .ie n .IP "$short" 4 .el .IP "\f(CW$short\fR" 4 .IX Item "$short" The short, exported name of the type. .ie n .IP "$full" 4 .el .IP "\f(CW$full\fR" 4 .IX Item "$full" The fully qualified name of this type as Moose knows it. .ie n .IP "$undef_message" 4 .el .IP "\f(CW$undef_message\fR" 4 .IX Item "$undef_message" A message that will be thrown when type functionality is used but the type does not yet exist. .SH "RECURSIVE SUBTYPES" .IX Header "RECURSIVE SUBTYPES" As of version 0.08, Moose::Types has experimental support for Recursive subtypes. This will allow: .PP .Vb 1 \& subtype Tree() => as HashRef[Str|Tree]; .Ve .PP Which validates things like: .PP .Vb 2 \& {key=>\*(Aqvalue\*(Aq}; \& {key=>{subkey1=>\*(Aqvalue\*(Aq, subkey2=>\*(Aqvalue\*(Aq}} .Ve .PP And so on. This feature is new and there may be lurking bugs so don't be afraid to hunt me down with patches and test cases if you have trouble. .SH "NOTES REGARDING TYPE UNIONS" .IX Header "NOTES REGARDING TYPE UNIONS" MooseX::Types uses MooseX::Types::TypeDecorator to do some overloading which generally allows you to easily create union types: .PP .Vb 2 \& subtype StrOrArrayRef, \& as Str|ArrayRef; .Ve .PP As with parameterized constraints, this overloading extends to modules using the types you define in a type library. .PP .Vb 2 \& use Moose; \& use MooseX::Types::Moose qw(HashRef Int); \& \& has \*(Aqattr\*(Aq => ( isa => HashRef | Int ); .Ve .PP And everything should just work as you'd think. .SH "METHODS" .IX Header "METHODS" .SS "import" .IX Subsection "import" Installs the MooseX::Types::Base class into the caller and exports types according to the specification described in \*(L"\s-1LIBRARY DEFINITION\*(R"\s0. This will continue to Moose::Util::TypeConstraints' \f(CW\*(C`import\*(C'\fR method to export helper functions you will need to declare your types. .SS "type_export_generator" .IX Subsection "type_export_generator" Generate a type export, e.g. \f(CW\*(C`Int()\*(C'\fR. This will return either a Moose::Meta::TypeConstraint object, or alternatively a MooseX::Types::UndefinedType object if the type was not yet defined. .ie n .SS "create_arged_type_constraint ($name, @args)" .el .SS "create_arged_type_constraint ($name, \f(CW@args\fP)" .IX Subsection "create_arged_type_constraint ($name, @args)" Given a String \f(CW$name\fR with \f(CW@args\fR find the matching type constraint and parameterize it with \f(CW@args\fR. .SS "create_base_type_constraint ($name)" .IX Subsection "create_base_type_constraint ($name)" Given a String \f(CW$name\fR, find the matching type constraint. .SS "create_type_decorator ($type_constraint)" .IX Subsection "create_type_decorator ($type_constraint)" Given a \f(CW$type_constraint\fR, return a lightweight MooseX::Types::TypeDecorator instance. .SS "coercion_export_generator" .IX Subsection "coercion_export_generator" This generates a coercion handler function, e.g. \f(CW\*(C`to_Int($value)\*(C'\fR. .SS "check_export_generator" .IX Subsection "check_export_generator" Generates a constraint check closure, e.g. \f(CW\*(C`is_Int($value)\*(C'\fR. .SH "CAVEATS" .IX Header "CAVEATS" The following are lists of gotchas and their workarounds for developers coming from the standard string based type constraint names .SS "Uniqueness" .IX Subsection "Uniqueness" 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. .SS "Argument separation ('=>' versus ',')" .IX Subsection "Argument separation ('=>' versus ',')" The perlop manpage has this to say about the '=>' operator: \*(L"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.\*(R" .PP Due to this stringification, the following will \s-1NOT\s0 work as you might think: .PP .Vb 1 \& subtype StrOrArrayRef => as Str | ArrayRef; .Ve .PP The \f(CW\*(C`StrOrArrayRef\*(C'\fR type will have its 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 authors of this package realize that all the Moose documentation and examples nearly uniformly use the '=>' version of the comma operator and this could be an issue if you are converting code. .PP Patches welcome for discussion. .SS "Compatibility with Sub::Exporter" .IX Subsection "Compatibility with Sub::Exporter" If you want to use Sub::Exporter with a Type Library, you need to make sure you export all the type constraints declared \s-1AS WELL AS\s0 any additional export targets. For example if you do: .PP .Vb 1 \& package TypeAndSubExporter; \& \& use MooseX::Types::Moose qw(Str); \& use MooseX::Types \-declare => [qw(MyStr)]; \& use Sub::Exporter \-setup => { exports => [qw(something)] }; \& \& subtype MyStr, as Str; \& \& sub something { \& return 1; \& } \& \& # then in another module ... \& \& package Foo; \& use TypeAndSubExporter qw(MyStr); .Ve .PP You'll get a \f(CW\*(C`"MyStr" is not exported by the TypeAndSubExporter module\*(C'\fR error. It can be worked around by: .PP .Vb 2 \& \- use Sub::Exporter \-setup => { exports => [ qw(something) ] }; \& + use Sub::Exporter \-setup => { exports => [ qw(something MyStr) ] }; .Ve .PP 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. .SH "COMBINING TYPE LIBRARIES" .IX Header "COMBINING TYPE LIBRARIES" You may want to combine a set of types for your application with other type libraries, like MooseX::Types::Moose or MooseX::Types::Common::String. .PP The MooseX::Types::Combine module provides a simple \s-1API\s0 for combining a set of type libraries together. .SH "SEE ALSO" .IX Header "SEE ALSO" Moose, Moose::Util::TypeConstraints, MooseX::Types::Moose, Sub::Exporter .SH "ACKNOWLEDGEMENTS" .IX Header "ACKNOWLEDGEMENTS" Many thanks to the \f(CW\*(C`#moose\*(C'\fR cabal on \f(CW\*(C`irc.perl.org\*(C'\fR. .SH "SUPPORT" .IX Header "SUPPORT" Bugs may be submitted through the \s-1RT\s0 bug tracker (or bug\-MooseX\-Types@rt.cpan.org ). .PP There is also a mailing list available for users of this distribution, at . .PP There is also an irc channel available for users of this distribution, at \&\f(CW\*(C`#moose\*(C'\fR on \f(CW\*(C`irc.perl.org\*(C'\fR . .SH "AUTHOR" .IX Header "AUTHOR" Robert \*(L"phaylon\*(R" Sedlacek .SH "CONTRIBUTORS" .IX Header "CONTRIBUTORS" .IP "\(bu" 4 Karen Etheridge .IP "\(bu" 4 Dave Rolsky .IP "\(bu" 4 John Napiorkowski .IP "\(bu" 4 Robert 'phaylon' Sedlacek .IP "\(bu" 4 Rafael Kitover .IP "\(bu" 4 Florian Ragwitz .IP "\(bu" 4 Matt S Trout .IP "\(bu" 4 Tomas Doran (t0m) .IP "\(bu" 4 Jesse Luehrs .IP "\(bu" 4 Mark Fowler .IP "\(bu" 4 Hans Dieter Pearcey .IP "\(bu" 4 Graham Knop .IP "\(bu" 4 Paul Fenwick .IP "\(bu" 4 Kent Fredric .IP "\(bu" 4 Justin Hunter .SH "COPYRIGHT AND LICENCE" .IX Header "COPYRIGHT AND LICENCE" This software is copyright (c) 2007 by Robert \*(L"phaylon\*(R" Sedlacek. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.