.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) .\" .\" 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 .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . 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 .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Types::Standard 3" .TH Types::Standard 3 2024-09-01 "perl v5.40.0" "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 Types::Standard \- bundled set of built\-in types for Type::Tiny .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 3 \& use v5.12; \& use strict; \& use warnings; \& \& package Horse { \& use Moo; \& use Types::Standard qw( Str Int Enum ArrayRef Object ); \& use Type::Params qw( compile ); \& use namespace::autoclean; \& \& has name => ( \& is => \*(Aqro\*(Aq, \& isa => Str, \& required => 1, \& ); \& has gender => ( \& is => \*(Aqro\*(Aq, \& isa => Enum[qw( f m )], \& ); \& has age => ( \& is => \*(Aqrw\*(Aq, \& isa => Int\->where( \*(Aq$_ >= 0\*(Aq ), \& ); \& has children => ( \& is => \*(Aqro\*(Aq, \& isa => ArrayRef[Object], \& default => sub { return [] }, \& ); \& \& sub add_child { \& state $check = signature( \& method => Object, \& positional => [ Object ], \& ); # method signature \& my ( $self, $child ) = $check\->( @_ ); # unpack @_ \& \& push @{ $self\->children }, $child; \& return $self; \& } \& } \& \& package main; \& \& my $boldruler = Horse\->new( \& name => "Bold Ruler", \& gender => \*(Aqm\*(Aq, \& age => 16, \& ); \& \& my $secretariat = Horse\->new( \& name => "Secretariat", \& gender => \*(Aqm\*(Aq, \& age => 0, \& ); \& \& $boldruler\->add_child( $secretariat ); \& \& use Types::Standard qw( is_Object assert_Object ); \& \& # is_Object($thing) returns a boolean \& my $is_it_an_object = is_Object($boldruler); \& \& # assert_Object($thing) returns $thing or dies \& say assert_Object($boldruler)\->name; # says "Bold Ruler" .Ve .SH STATUS .IX Header "STATUS" This module is covered by the Type-Tiny stability policy. .SH DESCRIPTION .IX Header "DESCRIPTION" This documents the details of the Types::Standard type library. Type::Tiny::Manual is a better starting place if you're new. .PP Type::Tiny bundles a few types which seem to be useful. .SS Moose-like .IX Subsection "Moose-like" The following types are similar to those described in Moose::Util::TypeConstraints. .IP \(bu 4 \&\fBAny\fR .Sp Absolutely any value passes this type constraint (even undef). .IP \(bu 4 \&\fBItem\fR .Sp Essentially the same as \fBAny\fR. All other type constraints in this library inherit directly or indirectly from \fBItem\fR. .IP \(bu 4 \&\fBBool\fR .Sp Values that are reasonable booleans. Accepts 1, 0, the empty string and undef. .Sp Other customers also bought: \fBBoolLike\fR from Types::TypeTiny. .IP \(bu 4 \&\fBMaybe[`a]\fR .Sp Given another type constraint, also accepts undef. For example, \&\fBMaybe[Int]\fR accepts all integers plus undef. .IP \(bu 4 \&\fBUndef\fR .Sp Only undef passes this type constraint. .IP \(bu 4 \&\fBDefined\fR .Sp Only undef fails this type constraint. .IP \(bu 4 \&\fBValue\fR .Sp Any defined, non-reference value. .IP \(bu 4 \&\fBStr\fR .Sp Any string. .Sp (The only difference between \fBValue\fR and \fBStr\fR is that the former accepts typeglobs and vstrings.) .Sp Other customers also bought: \fBStringLike\fR from Types::TypeTiny. .IP \(bu 4 \&\fBNum\fR .Sp See \fBLaxNum\fR and \fBStrictNum\fR below. .IP \(bu 4 \&\fBInt\fR .Sp An integer; that is a string of digits 0 to 9, optionally prefixed with a hyphen-minus character. .Sp Expect inconsistent results for dualvars, and numbers too high (or negative numbers too low) for Perl to safely represent as an integer. .IP \(bu 4 \&\fBClassName\fR .Sp The name of a loaded package. The package must have \f(CW@ISA\fR or \&\f(CW$VERSION\fR defined, or must define at least one sub to be considered a loaded package. .IP \(bu 4 \&\fBRoleName\fR .Sp Like \fBClassName\fR, but the package must \fInot\fR define a method called \&\f(CW\*(C`new\*(C'\fR. This is subtly different from Moose's type constraint of the same name; let me know if this causes you any problems. (I can't promise I'll change anything though.) .IP \(bu 4 \&\fBRef[`a]\fR .Sp Any defined reference value, including blessed objects. .Sp Unlike Moose, \fBRef\fR is a parameterized type, allowing Scalar::Util::reftype checks, a la .Sp .Vb 1 \& Ref["HASH"] # hashrefs, including blessed hashrefs .Ve .IP \(bu 4 \&\fBScalarRef[`a]\fR .Sp A value where \f(CW\*(C`ref($value) eq "SCALAR" or ref($value) eq "REF"\*(C'\fR. .Sp If parameterized, the referred value must pass the additional constraint. For example, \fBScalarRef[Int]\fR must be a reference to a scalar which holds an integer value. .IP \(bu 4 \&\fBArrayRef[`a]\fR .Sp A value where \f(CW\*(C`ref($value) eq "ARRAY"\*(C'\fR. .Sp If parameterized, the elements of the array must pass the additional constraint. For example, \fBArrayRef[Num]\fR must be a reference to an array of numbers. .Sp As an extension to Moose's \fBArrayRef\fR type, a minimum and maximum array length can be given: .Sp .Vb 3 \& ArrayRef[CodeRef, 1] # ArrayRef of at least one CodeRef \& ArrayRef[FileHandle, 0, 2] # ArrayRef of up to two FileHandles \& ArrayRef[Any, 0, 100] # ArrayRef of up to 100 elements .Ve .Sp Other customers also bought: \fBArrayLike\fR from Types::TypeTiny. .IP \(bu 4 \&\fBHashRef[`a]\fR .Sp A value where \f(CW\*(C`ref($value) eq "HASH"\*(C'\fR. .Sp If parameterized, the values of the hash must pass the additional constraint. For example, \fBHashRef[Num]\fR must be a reference to an hash where the values are numbers. The hash keys are not constrained, but Perl limits them to strings; see \fBMap\fR below if you need to further constrain the hash values. .Sp Other customers also bought: \fBHashLike\fR from Types::TypeTiny. .IP \(bu 4 \&\fBCodeRef\fR .Sp A value where \f(CW\*(C`ref($value) eq "CODE"\*(C'\fR. .Sp Other customers also bought: \fBCodeLike\fR from Types::TypeTiny. .IP \(bu 4 \&\fBRegexpRef\fR .Sp A reference where \f(CWre::is_regexp($value)\fR is true, or a blessed reference where \f(CW\*(C`$value\->isa("Regexp")\*(C'\fR is true. .IP \(bu 4 \&\fBGlobRef\fR .Sp A value where \f(CW\*(C`ref($value) eq "GLOB"\*(C'\fR. .IP \(bu 4 \&\fBFileHandle\fR .Sp A file handle. .IP \(bu 4 \&\fBObject\fR .Sp A blessed object. .Sp (This also accepts regexp refs.) .SS Structured .IX Subsection "Structured" Okay, so I stole some ideas from MooseX::Types::Structured. .IP \(bu 4 \&\fBMap[`k, `v]\fR .Sp Similar to \fBHashRef\fR but parameterized with type constraints for both the key and value. The constraint for keys would typically be a subtype of \&\fBStr\fR. .IP \(bu 4 \&\fBTuple[...]\fR .Sp Subtype of \fBArrayRef\fR, accepting a list of type constraints for each slot in the array. .Sp \&\fBTuple[Int, HashRef]\fR would match \f(CW\*(C`[1, {}]\*(C'\fR but not \f(CW\*(C`[{}, 1]\*(C'\fR. .IP \(bu 4 \&\fBDict[...]\fR .Sp Subtype of \fBHashRef\fR, accepting a list of type constraints for each slot in the hash. .Sp For example \fBDict[name => Str, id => Int]\fR allows \&\f(CW\*(C`{ name => "Bob", id => 42 }\*(C'\fR. .IP \(bu 4 \&\fBOptional[`a]\fR .Sp Used in conjunction with \fBDict\fR and \fBTuple\fR to specify slots that are optional and may be omitted (but not necessarily set to an explicit undef). .Sp \&\fBDict[name => Str, id => Optional[Int]]\fR allows \f(CW\*(C`{ name => "Bob" }\*(C'\fR but not \f(CW\*(C`{ name => "Bob", id => "BOB" }\*(C'\fR. .Sp Note that any use of \fBOptional[`a]\fR outside the context of parameterized \fBDict\fR and \fBTuple\fR type constraints makes little sense, and its behaviour is undefined. (An exception: it is used by Type::Params for a similar purpose to how it's used in \fBTuple\fR.) .PP This module also exports a \fBSlurpy\fR parameterized type, which can be used as follows. .PP It can cause additional trailing values in a \fBTuple\fR to be slurped into a structure and validated. For example, slurping into an arrayref: .PP .Vb 1 \& my $type = Tuple[ Str, Slurpy[ ArrayRef[Int] ] ]; \& \& $type\->( ["Hello"] ); # ok \& $type\->( ["Hello", 1, 2, 3] ); # ok \& $type\->( ["Hello", [1, 2, 3]] ); # not ok .Ve .PP Or into a hashref: .PP .Vb 1 \& my $type2 = Tuple[ Str, Slurpy[ Map[Int, RegexpRef] ] ]; \& \& $type2\->( ["Hello"] ); # ok \& $type2\->( ["Hello", 1, qr/one/i, 2, qr/two/] ); # ok .Ve .PP It can cause additional values in a \fBDict\fR to be slurped into a hashref and validated: .PP .Vb 1 \& my $type3 = Dict[ values => ArrayRef, Slurpy[ HashRef[Str] ] ]; \& \& $type3\->( { values => [] } ); # ok \& $type3\->( { values => [], name => "Foo" } ); # ok \& $type3\->( { values => [], name => [] } ); # not ok .Ve .PP In either \fBTuple\fR or \fBDict\fR, \fBSlurpy[Any]\fR can be used to indicate that additional values are acceptable, but should not be constrained in any way. .PP \&\fBSlurpy[Any]\fR is an optimized code path. Although the following are essentially equivalent checks, the former should run a lot faster: .PP .Vb 2 \& Tuple[ Int, Slurpy[Any] ] \& Tuple[ Int, Slurpy[ArrayRef] ] .Ve .PP A function \f(CWslurpy($type)\fR is also exported which was historically how slurpy types were created. .PP Outside of \fBDict\fR and \fBTuple\fR, \fBSlurpy[Foo]\fR should just act the same as \fBFoo\fR. But don't do that. .SS Objects .IX Subsection "Objects" Okay, so I stole some ideas from MooX::Types::MooseLike::Base. .IP \(bu 4 \&\fBInstanceOf[`a]\fR .Sp Shortcut for a union of Type::Tiny::Class constraints. .Sp \&\fBInstanceOf["Foo", "Bar"]\fR allows objects blessed into the \f(CW\*(C`Foo\*(C'\fR or \f(CW\*(C`Bar\*(C'\fR classes, or subclasses of those. .Sp Given no parameters, just equivalent to \fBObject\fR. .IP \(bu 4 \&\fBConsumerOf[`a]\fR .Sp Shortcut for an intersection of Type::Tiny::Role constraints. .Sp \&\fBConsumerOf["Foo", "Bar"]\fR allows objects where \f(CW\*(C`$o\->DOES("Foo")\*(C'\fR and \f(CW\*(C`$o\->DOES("Bar")\*(C'\fR both return true. .Sp Given no parameters, just equivalent to \fBObject\fR. .IP \(bu 4 \&\fBHasMethods[`a]\fR .Sp Shortcut for a Type::Tiny::Duck constraint. .Sp \&\fBHasMethods["foo", "bar"]\fR allows objects where \f(CW\*(C`$o\->can("foo")\*(C'\fR and \f(CW\*(C`$o\->can("bar")\*(C'\fR both return true. .Sp Given no parameters, just equivalent to \fBObject\fR. .SS More .IX Subsection "More" There are a few other types exported by this module: .IP \(bu 4 \&\fBOverload[`a]\fR .Sp With no parameters, checks that the value is an overloaded object. Can be given one or more string parameters, which are specific operations to check are overloaded. For example, the following checks for objects which overload addition and subtraction. .Sp .Vb 1 \& Overload["+", "\-"] .Ve .IP \(bu 4 \&\fBTied[`a]\fR .Sp A reference to a tied scalar, array or hash. .Sp Can be parameterized with a type constraint which will be applied to the object returned by the \f(CWtied()\fR function. As a convenience, can also be parameterized with a string, which will be inflated to a Type::Tiny::Class. .Sp .Vb 2 \& use Types::Standard qw(Tied); \& use Type::Utils qw(class_type); \& \& my $My_Package = class_type { class => "My::Package" }; \& \& tie my %h, "My::Package"; \& \e%h ~~ Tied; # true \& \e%h ~~ Tied[ $My_Package ]; # true \& \e%h ~~ Tied["My::Package"]; # true \& \& tie my $s, "Other::Package"; \& \e$s ~~ Tied; # true \& $s ~~ Tied; # false !! .Ve .Sp If you need to check that something is specifically a reference to a tied hash, use an intersection: .Sp .Vb 1 \& use Types::Standard qw( Tied HashRef ); \& \& my $TiedHash = (Tied) & (HashRef); \& \& tie my %h, "My::Package"; \& tie my $s, "Other::Package"; \& \& \e%h ~~ $TiedHash; # true \& \e$s ~~ $TiedHash; # false .Ve .IP \(bu 4 \&\fBStrMatch[`a]\fR .Sp A string that matches a regular expression: .Sp .Vb 2 \& declare "Distance", \& as StrMatch[ qr{^([0\-9]+)\es*(mm|cm|m|km)$} ]; .Ve .Sp You can optionally provide a type constraint for the array of subexpressions: .Sp .Vb 8 \& declare "Distance", \& as StrMatch[ \& qr{^([0\-9]+)\es*(.+)$}, \& Tuple[ \& Int, \& enum(DistanceUnit => [qw/ mm cm m km /]), \& ], \& ]; .Ve .Sp Here's an example using Regexp::Common: .Sp .Vb 10 \& package Local::Host { \& use Moose; \& use Regexp::Common; \& has ip_address => ( \& is => \*(Aqro\*(Aq, \& required => 1, \& isa => StrMatch[qr/^$RE{net}{IPv4}$/], \& default => \*(Aq127.0.0.1\*(Aq, \& ); \& } .Ve .Sp On certain versions of Perl, type constraints of the forms \&\fBStrMatch[qr/../\fR and \fBStrMatch[qr/\eA..\ez/\fR with any number of intervening dots can be optimized to simple length checks. .IP \(bu 4 \&\fBEnum[`a]\fR .Sp As per MooX::Types::MooseLike::Base: .Sp .Vb 4 \& has size => ( \& is => "ro", \& isa => Enum[qw( S M L XL XXL )], \& ); .Ve .Sp You can enable coercion by passing \f(CW\*(C`\e1\*(C'\fR before the list of values. .Sp .Vb 5 \& has size => ( \& is => "ro", \& isa => Enum[ \e1, qw( S M L XL XXL ) ], \& coerce => 1, \& ); .Ve .Sp This will use the \f(CW\*(C`closest_match\*(C'\fR method in Type::Tiny::Enum to coerce closely matching strings. .IP \(bu 4 \&\fBOptList\fR .Sp An arrayref of arrayrefs in the style of Data::OptList output. .IP \(bu 4 \&\fBLaxNum\fR, \fBStrictNum\fR .Sp In Moose 2.09, the \fBNum\fR type constraint implementation was changed from being a wrapper around Scalar::Util's \f(CW\*(C`looks_like_number\*(C'\fR function to a stricter regexp (which disallows things like "\-Inf" and "Nan"). .Sp Types::Standard provides \fIboth\fR implementations. \fBLaxNum\fR is measurably faster. .Sp The \fBNum\fR type constraint is currently an alias for \fBLaxNum\fR unless you set the \f(CW\*(C`PERL_TYPES_STANDARD_STRICTNUM\*(C'\fR environment variable to true before loading Types::Standard, in which case it becomes an alias for \fBStrictNum\fR. The constant \f(CW\*(C`Types::Standard::STRICTNUM\*(C'\fR can be used to check if \&\fBNum\fR is being strict. .Sp Most people should probably use \fBNum\fR or \fBStrictNum\fR. Don't explicitly use \fBLaxNum\fR unless you specifically need an attribute which will accept things like "Inf". .IP \(bu 4 \&\fBCycleTuple[`a]\fR .Sp Similar to \fBTuple\fR, but cyclical. .Sp .Vb 1 \& CycleTuple[Int, HashRef] .Ve .Sp will allow \f(CW\*(C`[1,{}]\*(C'\fR and \f(CW\*(C`[1,{},2,{}]\*(C'\fR but disallow \&\f(CW\*(C`[1,{},2]\*(C'\fR and \f(CW\*(C`[1,{},2,[]]\*(C'\fR. .Sp I think you understand \fBCycleTuple\fR already. .Sp Currently \fBOptional\fR and \fBSlurpy\fR parameters are forbidden. There are fairly limited use cases for them, and it's not exactly clear what they should mean. .Sp The following is an efficient way of checking for an even-sized arrayref: .Sp .Vb 1 \& CycleTuple[Any, Any] .Ve .Sp The following is an arrayref which would be suitable for coercing to a hashref: .Sp .Vb 1 \& CycleTuple[Str, Any] .Ve .Sp All the examples so far have used two parameters, but the following is also a possible \fBCycleTuple\fR: .Sp .Vb 1 \& CycleTuple[Str, Int, HashRef] .Ve .Sp This will be an arrayref where the 0th, 3rd, 6th, etc values are strings, the 1st, 4th, 7th, etc values are integers, and the 2nd, 5th, 8th, etc values are hashrefs. .SS Coercions .IX Subsection "Coercions" Most of the types in this type library have no coercions. The exception is \fBBool\fR as of Types::Standard 1.003_003, which coerces from \fBAny\fR via \f(CW\*(C`!!$_\*(C'\fR. .PP Some standalone coercions may be exported. These can be combined with type constraints using the \f(CW\*(C`plus_coercions\*(C'\fR method. .IP \(bu 4 \&\fBMkOpt\fR .Sp A coercion from \fBArrayRef\fR, \fBHashRef\fR or \fBUndef\fR to \fBOptList\fR. Example usage in a Moose attribute: .Sp .Vb 1 \& use Types::Standard qw( OptList MkOpt ); \& \& has options => ( \& is => "ro", \& isa => OptList\->plus_coercions( MkOpt ), \& coerce => 1, \& ); .Ve .IP \(bu 4 \&\fBSplit[`a]\fR .Sp Split a string on a regexp. .Sp .Vb 1 \& use Types::Standard qw( ArrayRef Str Split ); \& \& has name => ( \& is => "ro", \& isa => ArrayRef\->of(Str)\->plus_coercions(Split[qr/\es/]), \& coerce => 1, \& ); .Ve .IP \(bu 4 \&\fBJoin[`a]\fR .Sp Join an array of strings with a delimiter. .Sp .Vb 1 \& use Types::Standard qw( Str Join ); \& \& my $FileLines = Str\->plus_coercions(Join["\en"]); \& \& has file_contents => ( \& is => "ro", \& isa => $FileLines, \& coerce => 1, \& ); .Ve .SS Constants .IX Subsection "Constants" .ie n .IP """Types::Standard::STRICTNUM""" 4 .el .IP \f(CWTypes::Standard::STRICTNUM\fR 4 .IX Item "Types::Standard::STRICTNUM" Indicates whether \fBNum\fR is an alias for \fBStrictNum\fR. (It is usually an alias for \fBLaxNum\fR.) .SS Environment .IX Subsection "Environment" .ie n .IP """PERL_TYPES_STANDARD_STRICTNUM""" 4 .el .IP \f(CWPERL_TYPES_STANDARD_STRICTNUM\fR 4 .IX Item "PERL_TYPES_STANDARD_STRICTNUM" Switches to more strict regexp-based number checking instead of using \&\f(CW\*(C`looks_like_number\*(C'\fR. .ie n .IP """PERL_TYPE_TINY_XS""" 4 .el .IP \f(CWPERL_TYPE_TINY_XS\fR 4 .IX Item "PERL_TYPE_TINY_XS" If set to false, can be used to suppress the loading of XS implementions of some type constraints. .ie n .IP """PERL_ONLY""" 4 .el .IP \f(CWPERL_ONLY\fR 4 .IX Item "PERL_ONLY" If \f(CW\*(C`PERL_TYPE_TINY_XS\*(C'\fR does not exist, can be set to true to suppress XS usage similarly. (Several other CPAN distributions also pay attention to this environment variable.) .SH BUGS .IX Header "BUGS" Please report any bugs to . .SH "SEE ALSO" .IX Header "SEE ALSO" The Type::Tiny homepage . .PP Type::Tiny::Manual. .PP Type::Tiny, Type::Library, Type::Utils, Type::Coercion. .PP Moose::Util::TypeConstraints, Mouse::Util::TypeConstraints, MooseX::Types::Structured. .PP Types::XSD provides some type constraints based on XML Schema's data types; this includes constraints for ISO8601\-formatted datetimes, integer ranges (e.g. \fBPositiveInteger[maxInclusive=>10]\fR and so on. .PP Types::Encodings provides \fBBytes\fR and \fBChars\fR type constraints that were formerly found in Types::Standard. .PP Types::Common::Numeric and Types::Common::String provide replacements for MooseX::Types::Common. .SH AUTHOR .IX Header "AUTHOR" Toby Inkster . .SH "COPYRIGHT AND LICENCE" .IX Header "COPYRIGHT AND LICENCE" This software is copyright (c) 2013\-2014, 2017\-2023 by Toby Inkster. .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. .SH "DISCLAIMER OF WARRANTIES" .IX Header "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.