CONTENTS

NAME

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

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.

Objects

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

More

There are a few other types exported by this module:

Coercions

Most of the types in this type library have no coercions. The exception is Bool as of Types::Standard 1.003_003, which coerces from Any via !!$_.

Some standalone coercions may be exported. These can be combined with type constraints using the plus_coercions method.

Constants

Types::Standard::STRICTNUM

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

Environment

PERL_TYPES_STANDARD_STRICTNUM

Switches to more strict regexp-based number checking instead of using looks_like_number.

PERL_TYPE_TINY_XS

If set to false, can be used to suppress the loading of XS implementations of some type constraints.

PERL_ONLY

If PERL_TYPE_TINY_XS does not exist, can be set to true to suppress XS usage similarly. (Several other CPAN distributions also pay attention to this environment variable.)

BUGS

Please report any bugs to https://github.com/tobyink/p5-type-tiny/issues.

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.

AUTHOR

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE

This software is copyright (c) 2013-2014, 2017-2024 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.