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

NAME

Type::Utils - utility functions to make defining and using type constraints a little easier

SYNOPSIS

   package Types::Mine;
   
   use Type::Library -base;
   use Type::Utils -all;
   
   BEGIN { extends "Types::Standard" };
   
   declare "AllCaps",
      as "Str",
      where { uc($_) eq $_ },
      inline_as { my $varname = $_[1]; "uc($varname) eq $varname" };
   
   coerce "AllCaps",
      from "Str", via { uc($_) };

DESCRIPTION

This module provides utility functions to make defining and using type constraints a little easier.

Type declaration functions

Many of the following are similar to the similarly named functions described in Moose::Util::TypeConstraints.

declare $name, %options
declare %options

Declare a named or anonymous type constraint. Use as and where to specify the parent type (if any) and (possibly) refine its definition.

   declare EvenInt, as Int, where { $_ % 2 == 0 };

   my $EvenInt = declare as Int, where { $_ % 2 == 0 };

NOTE: If the caller package inherits from Type::Library then any non-anonymous types declared in the package will be automatically installed into the library.

Hidden gem: if you're inheriting from a type constraint that includes some coercions, you can include coerce => 1 in the %options hash to inherit the coercions.

subtype $name, %options
subtype %options

Declare a named or anonymous type constraint which is descended from an existing type constraint. Use as and where to specify the parent type and refine its definition.

Actually, you should use declare instead; this is just an alias.

This function is not exported by default.

type $name, %options
type %options

Declare a named or anonymous type constraint which is not descended from an existing type constraint. Use where to provide a coderef that constrains values.

Actually, you should use declare instead; this is just an alias.

This function is not exported by default.

as $parent

Used with declare to specify a parent type constraint:

   declare EvenInt, as Int, where { $_ % 2 == 0 };
where { BLOCK }

Used with declare to provide the constraint coderef:

   declare EvenInt, as Int, where { $_ % 2 == 0 };

The coderef operates on $_, which is the value being tested.

message { BLOCK }

Generate a custom error message when a value fails validation.

   declare EvenInt,
      as Int,
      where { $_ % 2 == 0 },
      message {
         if (Int->check($_))
            { "$_ is an integer, but not even" }
         else
            { "$_ is not an integer" }
      };
inline_as { BLOCK }

Generate a string of Perl code that can be used to inline the type check into other functions. If your type check is being used within a Moose or Moo constructor or accessor methods, or used by Type::Params, this can lead to significant performance improvements.

   declare EvenInt,
      as Int,
      where { $_ % 2 == 0 },
      inline_as {
         my ($constraint, $varname) = @_;
         my $perlcode = 
            $constraint->parent->inline_check($varname)
            . "&& ($varname % 2 == 0)";
         return $perlcode;
      };
   
   warn EvenInt->inline_check('$xxx');  # demonstration
class_type $name, { class => $package, %options }
class_type { class => $package, %options }

Shortcut for declaring a Type::Tiny::Class type constraint.

role_type $name, { role => $package, %options }
role_type { role => $package, %options }

Shortcut for declaring a Type::Tiny::Role type constraint.

duck_type $name, \@methods
duck_type \@methods

Shortcut for declaring a Type::Tiny::Duck type constraint.

union $name, \@constraints
union \@constraints

Shortcut for declaring a Type::Tiny::Union type constraint.

enum $name, \@values
enum \@values

Shortcut for declaring a Type::Tiny::Enum type constraint.

intersection $name, \@constraints
intersection \@constraints

Shortcut for declaring a Type::Tiny::Intersection type constraint.

Coercion declaration functions

Many of the following are similar to the similarly named functions described in Moose::Util::TypeConstraints.

coerce $target, @coercions

Add coercions to the target type constraint. The list of coercions is a list of type constraint, conversion code pairs. Conversion code can be either a string of Perl code or a coderef; in either case the value to be converted is $_.

from $source

Sugar to specify a type constraint in a list of coercions:

   coerce EvenInt, from Int, via { $_ * 2 };  # As a coderef...
   coerce EvenInt, from Int, q { $_ * 2 };    # or as a string!
via { BLOCK }

Sugar to specify a coderef in a list of coercions.

declare_coercion $name, \%opts, $type1, $code1, ...
declare_coercion \%opts, $type1, $code1, ...

Declares a coercion that is not explicitly attached to any type in the library. For example:

   declare_coercion "ArrayRefFromAny", from "Any", via { [$_] };

This coercion will be exportable from the library as a Type::Coercion object, but the ArrayRef type exported by the library won't automatically use it.

Coercions declared this way are immutable (frozen).

to_type $type

Used with declare_coercion to declare the target type constraint for a coercion, but still without explicitly attaching the coercion to the type constraint:

   declare_coercion "ArrayRefFromAny",
      to_type "ArrayRef",
      from "Any", via { [$_] };

You should pretty much always use this when declaring an unattached coercion because it's exceedingly useful for a type coercion to know what it will coerce to - this allows it to skip coercion when no coercion is needed (e.g. avoiding coercing [] to [ [] ]) and allows assert_coerce to work properly.

Type library management

extends @libraries

Indicates that this type library extends other type libraries, importing their type constraints.

Should usually be executed in a BEGIN block.

This is not exported by default because it's not fun to export it to Moo, Moose or Mouse classes! use Type::Utils -all can be used to import it into your type library.

Other

match_on_type $value => ($type => \&action, ..., \&default?)

Something like a switch/case or given/when construct. Dispatches along different code paths depending on the type of the incoming value. Example blatantly stolen from the Moose documentation:

   sub to_json
   {
      my $value = shift;
      
      return match_on_type $value => (
         HashRef() => sub {
            my $hash = shift;
            '{ '
               . (
               join ", " =>
               map { '"' . $_ . '" : ' . to_json( $hash->{$_} ) }
               sort keys %$hash
            ) . ' }';
         },
         ArrayRef() => sub {
            my $array = shift;
            '[ '.( join ", " => map { to_json($_) } @$array ).' ]';
         },
         Num()   => q {$_},
         Str()   => q { '"' . $_ . '"' },
         Undef() => q {'null'},
         => sub { die "$_ is not acceptable json type" },
      );
   }

Note that unlike Moose, code can be specified as a string instead of a coderef. (e.g. for Num, Str and Undef above.)

For improved performance, try compile_match_on_type.

This function is not exported by default.

my $coderef = compile_match_on_type($type => \&action, ..., \&default?)

Compile a match_on_type block into a coderef. The following JSON converter is about two orders of magnitude faster than the previous example:

   sub to_json;
   *to_json = compile_match_on_type(
      HashRef() => sub {
         my $hash = shift;
         '{ '
            . (
            join ", " =>
            map { '"' . $_ . '" : ' . to_json( $hash->{$_} ) }
            sort keys %$hash
         ) . ' }';
      },
      ArrayRef() => sub {
         my $array = shift;
         '[ '.( join ", " => map { to_json($_) } @$array ).' ]';
      },
      Num()   => q {$_},
      Str()   => q { '"' . $_ . '"' },
      Undef() => q {'null'},
      => sub { die "$_ is not acceptable json type" },
   );

Remember to store the coderef somewhere fairly permanent so that you don't compile it over and over. state variables (in Perl >= 5.10) are good for this. (Same sort of idea as Type::Params.)

This function is not exported by default.

EXPORT

By default, all of the functions documented above are exported, except subtype and type (prefer declare instead), extends, and match_on_type/compile_match_on_type.

This module uses Exporter::TypeTiny; see the documentation of that module for tips and tricks importing from Type::Utils.

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, Types::Standard, Type::Coercion.

Type::Tiny::Class, Type::Tiny::Role, Type::Tiny::Duck, Type::Tiny::Enum, Type::Tiny::Union.

Moose::Util::TypeConstraints, Mouse::Util::TypeConstraints.

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.