Type::Tiny::Manual - an overview of Type::Tiny
Type::Tiny is a small class for writing type constraints, inspired by Moose's type constraint API. It has only one non-core dependency (and even that is simply a module that was previously distributed as part of Type::Tiny but has since been spun off), and can be used with Moose, Mouse and Moo (or none of the above).
Type::Tiny is bundled with Type::Library a framework for organizing type constraints into collections.
Also bundled is Types::Standard, a Moose-inspired library of useful type constraints.
Type::Params is also provided, to allow very fast checking and coercion of function and method parameters.
Even if you are using Type::Tiny with other object-oriented programming toolkits (such as Moose or Mouse), you should start with the Moo sections of the manual. Most of the information is directly transferrable and the Moose and Mouse sections of the manual list the minor differences between using Type::Tiny with Moo and with them.
In general, this manual assumes you use Perl 5.12 or above and may use examples that do not work on older versions of Perl. Type::Tiny does work on earlier versions of Perl, but not all the examples and features in the manual will run without adjustment. (For instance, you may need to replace state variables with lexical variables, avoid the package NAME { BLOCK } syntax, etc.)
state
package NAME { BLOCK }
Installing Type-Tiny should be straightforward.
If you have cpanm, you only need one line:
% cpanm Type::Tiny
If you are installing into a system-wide directory, you may need to pass the "-S" flag to cpanm, which uses sudo to install the module:
% cpanm -S Type::Tiny
Alternatively, if your CPAN shell is set up, you should just be able to do:
% cpan Type::Tiny
As a last resort, you can manually install it. Download the tarball and unpack it.
Consult the file META.json for a list of pre-requisites. Install these first.
To build Type-Tiny:
% perl Makefile.PL % make && make test
Then install it:
% make install
If you are installing into a system-wide directory, you may need to run:
% sudo make install
Type::Tiny requires at least Perl 5.6.1, though certain Unicode-related features (e.g. non-ASCII type constraint names) may work better in newer versions of Perl.
Type::Tiny requires Exporter::Tiny, a module that was previously bundled in this distribution, but has since been spun off as a separate distribution. Don't worry - it's quick and easy to install.
At run-time, Type::Tiny also requires the following modules: B, B::Deparse, Carp, Data::Dumper, Scalar::Util, Text::Balanced, overload, strict and warnings. All of these come bundled with Perl itself. Prior to Perl 5.8, Scalar::Util and Text::Balanced do not come bundled with Perl and will need installing separately from the CPAN.
Certain features require additional modules. Tying a variable to a type constraint (e.g. tie my $count, Int) requires Type::Tie; stack traces on exceptions require Devel::StackTrace. The Reply::Plugin::TypeTiny plugin for Reply requires Reply (obviously). Devel::LexAlias may slightly increase the speed of some of Type::Tiny's compiled coderefs.
tie my $count, Int
Type::Tiny::XS is not required, but if available provides a speed boost for some type checks. (Setting the environment variable PERL_TYPE_TINY_XS to false, or setting PERL_ONLY to true will suppress the use of Type::Tiny::XS, even if it is available.)
PERL_TYPE_TINY_XS
PERL_ONLY
The test suite additionally requires Test::More, Test::Fatal and Test::Requires. Test::More comes bundled with Perl, but if you are using a version of Perl older than 5.14, you will need to upgrade to at least Test::More version 0.96. Test::Requires and Test::Fatal (plus Try::Tiny which Test::Fatal depends on) are bundled with Type::Tiny in the inc directory, so you do not need to install them separately.
inc
If using Type::Tiny in conjunction with Moo, then at least Moo 1.006000 is recommended. If using Type::Tiny with Moose, then at least Moose 2.0000 is recommended. If using Type::Tiny with Mouse, then at least Mouse 1.00 is recommended. Type::Tiny is mostly untested against older versions of these packages.
Type::Tiny::Manual::UsingWithMoo
Basic use of Type::Tiny with Moo, including attribute type constraints, parameterized type constraints, coercions, and method parameter checking.
Type::Tiny::Manual::UsingWithMoo2
Advanced use of Type::Tiny with Moo, including unions and intersections, stringifies_to, numifies_to, with_attribute_values, and where.
stringifies_to
numifies_to
with_attribute_values
where
Type::Tiny::Manual::UsingWithMoo3
There's more than one way to do it! Alternative ways of using Type::Tiny, including type registries, exported functions, and dwim_type.
dwim_type
Type::Tiny::Manual::Libraries
Defining your own type libraries, including extending existing libraries, defining new types, adding coercions, defining parameterizable types, and the declarative style.
Type::Tiny::Manual::UsingWithMoose
How to use Type::Tiny with Moose, including the advantages of Type::Tiny over built-in type constraints, and Moose-specific features.
Type::Tiny::Manual::UsingWithMouse
How to use Type::Tiny with Mouse, including the advantages of Type::Tiny over built-in type constraints, and Mouse-specific features.
Type::Tiny::Manual::UsingWithClassTiny
Including how to Type::Tiny in your object's BUILD method, and third-party shims between Type::Tiny and Class::Tiny.
BUILD
Type::Tiny::Manual::UsingWithOther
Using Type::Tiny with Class::InsideOut, Params::Check, and Object::Accessor.
Type::Tiny::Manual::UsingWithTestMore
Type::Tiny for test suites.
Type::Tiny::Manual::Params
Advanced information on Type::Params, and using Type::Tiny with other signature modules like Function::Parameters and Kavorka.
Type::Tiny::Manual::NonOO
Type::Tiny in non-object-oriented code.
Type::Tiny::Manual::Optimization
Squeeze the most out of your CPU.
Type::Tiny::Manual::Coercions
Advanced information on coercions.
Type::Tiny::Manual::AllTypes
An alphabetical list of all type constraints bundled with Type::Tiny.
Type::Tiny::Manual::Policies
Policies related to Type::Tiny development.
Type::Tiny is similar in aim to Specio. The major differences are
Type::Tiny is "tiny" (Specio will eventually have fewer dependencies than it currently does, but is unlikely to ever have as few as Type::Tiny);
Specio has a somewhat nicer API (better method names; less duplication), and its API is likely to improve further. Type::Tiny's aims at complete compatibility with current versions of Moose and Mouse, so there is a limit to how much I can deviate from the existing APIs of (Moose|Mouse)::Meta::TypeConstraint.
Type::Tiny libraries expose a similar interface to MooseX::Types libraries. In most cases you should be able to rewrite a MooseX::Types library to use Type::Tiny pretty easily.
Type::Tiny is faster and supports coercions.
Scalar::Does is somewhat of a precursor to Type::Tiny, but has now been rewritten to use Type::Tiny internally.
It gives you a does($value, $type) function that is roughly equivalent to $type->check($value) except that $type may be one of a list of pre-defined strings (instead of a Type::Tiny type constraint); or may be a package name in which case it will be assumed to be a role and checked with $value->DOES($type).
does($value, $type)
$type->check($value)
$type
$value->DOES($type)
Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=Type-Tiny.
IRC: support is available through in the #moops channel on irc.perl.org.
Toby Inkster <tobyink@cpan.org>.
This software is copyright (c) 2013-2014, 2017-2019 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.
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.
To install Type::Tiny, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Type::Tiny
CPAN shell
perl -MCPAN -e shell install Type::Tiny
For more information on module installation, please visit the detailed CPAN module installation guide.