Types::Standard

Synopsis

 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       => 'ro',
     isa      => Str,
     required => 1,
   );
   has gender => (
     is       => 'ro',
     isa      => Enum[qw( f m )],
   );
   has age => (
     is       => 'rw',
     isa      => Int->where( '$_ >= 0' ),
   );
   has children => (
     is       => 'ro',
     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  => 'm',
   age     => 16,
 );
 
 my $secretariat = Horse->new(
   name    => "Secretariat",
   gender  => 'm',
   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"

Status

This module is covered by the Type-Tiny stability policy .

Description

This documents the details of the Types::Standard type library. Type::Tiny::Manual is a better starting place if you're new.

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 .

Structured

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

This module also exports a Slurpy parameterized type, which can be used as follows.

It can cause additional trailing values in a Tuple to be slurped into a structure and validated. For example, slurping into an arrayref:

   my $type = Tuple[ Str, Slurpy[ ArrayRef[Int] ] ];
   
   $type->( ["Hello"] );                # ok
   $type->( ["Hello", 1, 2, 3] );       # ok
   $type->( ["Hello", [1, 2, 3]] );     # not ok

Or into a hashref:

   my $type2 = Tuple[ Str, Slurpy[ Map[Int, RegexpRef] ] ];
   
   $type2->( ["Hello"] );                               # ok
   $type2->( ["Hello", 1, qr/one/i, 2, qr/two/] );      # ok

It can cause additional values in a Dict to be slurped into a hashref and validated:

   my $type3 = Dict[ values => ArrayRef, Slurpy[ HashRef[Str] ] ];
   
   $type3->( { values => [] } );                        # ok
   $type3->( { values => [], name => "Foo" } );         # ok
   $type3->( { values => [], name => [] } );            # not ok

In either Tuple or Dict , Slurpy[Any] can be used to indicate that additional values are acceptable, but should not be constrained in any way.

Slurpy[Any] is an optimized code path. Although the following are essentially equivalent checks, the former should run a lot faster:

   Tuple[ Int, Slurpy[Any] ]
   Tuple[ Int, Slurpy[ArrayRef] ]

A function slurpy($type) is also exported which was historically how slurpy types were created.

Outside of Dict and Tuple , Slurpy[Foo] should just act the same as Foo . But don't do that.

See Also

The Type::Tiny homepage .

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.

Types::Common::Numeric and Types::Common::String provide replacements for MooseX::Types::Common .