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

NAME

Types::Standard - bundled set of built-in types for Type::Tiny

DESCRIPTION

Type::Tiny bundles a few types which seem to be useful.

Moose-like

The following types are similar to those described in Moose::Util::TypeConstraints.

Any
Item
Bool
Maybe[`a]
Undef
Defined
Value
Str
Num
Int
ClassName
RoleName
Ref[`a]
ScalarRef[`a]
ArrayRef[`a]
HashRef[`a]
CodeRef
RegexpRef
GlobRef
FileHandle
Object

Unlike Moose, Ref is a parameterized type, allowing Scalar::Util::reftype checks, a la

   Ref["HASH"]  # hashrefs, including blessed hashrefs

Structured

OK, so I stole some ideas from MooseX::Types::Structured.

Map[`a]
Tuple[`a]
Dict[`a]
Optional[`a]

This module also exports a slurpy function.

Objects

OK, so I stole some ideas from MooX::Types::MooseLike::Base.

InstanceOf[`a]
ConsumerOf[`a]
HasMethods[`a]

More

There are a few other types exported by this function:

Overload[`a]

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.

   Overload["+", "-"]
Tied[`a]

A reference to a tied scalar, array or hash.

Can be parameterized with a type constraint which will be applied to the object returned by the tied() function. As a convenience, can also be parameterized with a string, which will be inflated to a Type::Tiny::Class.

   use Types::Standard qw(Tied);
   use Type::Utils qw(class_type);
   
   my $My_Package = class_type { class => "My::Package" };
   
   tie my %h, "My::Package";
   \%h ~~ Tied;                   # true
   \%h ~~ Tied[ $My_Package ];    # true
   \%h ~~ Tied["My::Package"];    # true
   
   tie my $s, "Other::Package";
   \$s ~~ Tied;                   # true
   $s  ~~ Tied;                   # false !!

If you need to check that something is specifically a reference to a tied hash, use an intersection:

   use Types::Standard qw( Tied HashRef );
   
   my $TiedHash = (Tied) & (HashRef);
   
   tie my %h, "My::Package";
   tie my $s, "Other::Package";
   
   \%h ~~ $TiedHash;     # true
   \$s ~~ $TiedHash;     # false
StrMatch[`a]

A string that matches a regular exception:

   declare "Distance",
      as StrMatch[ qr{^([0-9]+)\s*(mm|cm|m|km)$} ];

You can optionally provide a type constraint for the array of subexpressions:

   declare "Distance",
      as StrMatch[
         qr{^([0-9]+)\s*(.+)$},
         Tuple[
            Int,
            enum(DistanceUnit => [qw/ mm cm m km /]),
         ],
      ];
Enum[`a]

As per MooX::Types::MooseLike::Base:

   has size => (is => "ro", isa => Enum[qw( S M L XL XXL )]);
OptList

An arrayref of arrayrefs in the style of Data::OptList output.

LaxNum, StrictNum

In Moose 2.09, the Num type constraint implementation was changed from being a wrapper around Scalar::Util's looks_like_number function to a stricter regexp (which disallows things like "-Inf" and "Nan").

Types::Standard provides both implementations. LaxNum is measurably faster.

The Num type constraint is currently an alias for LaxNum unless you set the PERL_TYPES_STANDARD_STRICTNUM environment variable to true before loading Types::Standard, in which case it becomes an alias for StrictNum. The constant Types::Standard::STRICTNUM can be used to check if Num is being strict.

Most people should probably use Num or StrictNum. Don't explicitly use LaxNum unless you specifically need an attribute which will accept things like "Inf".

Coercions

None of the types in this type library have any coercions by default. However some standalone coercions may be exported. These can be combined with type constraints using the + operator.

MkOpt

A coercion from ArrayRef, HashRef or Undef to OptList. Example usage in a Moose attribute:

   use Types::Standard qw( OptList MkOpt );
   
   has options => (
      is     => "ro",
      isa    => OptList + MkOpt,
      coerce => 1,
   );
Split[`a]

Split a string on a regexp.

   use Types::Standard qw( ArrayRef Str Split );
   
   has name => (
      is     => "ro",
      isa    => (ArrayRef[Str]) + (Split[qr/\s/]),
      coerce => 1,
   );
Join[`a]

Join an array of strings with a delimiter.

   use Types::Standard qw( Str Join );
   
   my $FileLines = Str + Join["\n"];
   
   has file_contents => (
      is     => "ro",
      isa    => $FileLines,
      coerce => 1,
   );

Constants

Types::Standard::STRICTNUM

Indicates whether Num is an alias for StrictNum. (It is usually an alias for LaxNum.)

BUGS

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

SEE ALSO

Type::Tiny::Manual.

Type::Tiny, Type::Library, Type::Utils, Type::Coercion.

Moose::Util::TypeConstraints, Mouse::Util::TypeConstraints, MooseX::Types::Structured.

Types::XSD provides some type constraints based on XML Schema's data types; this includes constraints for ISO8601-formatted datetimes, integer ranges (e.g. PositiveInteger[maxInclusive=>10] and so on.

Types::Encodings provides Bytes and Chars type constraints that were formerly found in Types::Standard.

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.