perldoc > Types::Standard

🏷️ NAME

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

🚀 Quick Reference

Use CaseCommandDescription
Declare a string attributeStrAny string value
Declare an integer attributeIntInteger number (string of digits, optional minus)
Declare a boolean attributeBoolAccepts 1, 0, empty string, undef
Declare an optional attributeMaybe[Int]Accepts Int or undef
Declare an array of objectsArrayRef[Object]Arrayref where each element is an object
Declare a hash of numbersHashRef[Num]Hashref where all values are numbers
Validate method signatureuse Type::Params qw(compile)Compile and check method arguments
Check a fixed-position tupleTuple[Int, HashRef]Arrayref of exact length with specified types
Check a named dictionaryDict[name => Str, id => Int]Hashref with specific keys and types
Check an object’s classInstanceOf["Foo", "Bar"]Object blessed into Foo or Bar or subclass
Check an object’s roleConsumerOf["Foo", "Bar"]Object that does() both roles

📖 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 = compile( Object, 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

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

This module also exports a slurpy function, 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] vs Tuple[Int, slurpy ArrayRef].

🔹 Objects

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

🔹 More

There are a few other types exported by this module:

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
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 /]),
      ],
   ];

Here's an example using Regexp::Common:

package Local::Host {
   use Moose;
   use Regexp::Common;
   has ip_address => (
      is         => 'ro',
      required   => 1,
      isa        => StrMatch[qr/^$RE{net}{IPv4}$/],
      default    => '127.0.0.1',
   );
}

On certain versions of Perl, type constraints of the forms StrMatch[qr/../ and StrMatch[qr/\A..\z/ with any number of intervening dots can be optimized to simple length checks.

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

You can enable coercion by passing \1 before the list of values.

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

This will use the closest_match method in Type::Tiny::Enum to coerce closely matching strings.

🔹 Coercions

Most of the types in this type library have no coercions by default. 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.

use Types::Standard qw( OptList MkOpt );

has options => (
   is     => "ro",
   isa    => OptList->plus_coercions( MkOpt ),
   coerce => 1,
);
use Types::Standard qw( ArrayRef Str Split );

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

my $FileLines = Str->plus_coercions(Join["\n"]);

has file_contents => (
   is     => "ro",
   isa    => $FileLines,
   coerce => 1,
);

🔹 Constants

🔹 Environment

🐛 BUGS

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

📚 SEE ALSO

The Type::Tiny homepage <https://typetiny.toby.ink/>.

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 AT cpan.org>.

📜 COPYRIGHT AND LICENCE

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

Types::Standard
🏷️ NAME 🚀 Quick Reference 📖 SYNOPSIS 📊 STATUS 📝 DESCRIPTION
🔹 Moose-like 🔹 Structured 🔹 Objects 🔹 More 🔹 Coercions 🔹 Constants 🔹 Environment
🐛 BUGS 📚 SEE ALSO 👤 AUTHOR 📜 COPYRIGHT AND LICENCE ⚠️ DISCLAIMER OF WARRANTIES

Generated by phpman v4.9.26-5-g7740029 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-17 16:31 @216.73.217.35
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!

^_top_^