Exploring Type::Tiny Part 6: Some Interesting Type Libraries

This is an old article. Some links may be broken. Some of the information may no longer be correct. Opinions expressed in this article may no longer be held.

Type::Tiny is probably best known as a way of having Moose-like type constraints in Moo, but it can be used for so much more. This is the sixth in a series of posts showing other things you can use Type::Tiny for. This article along with the earlier ones in the series can be found on my blog and in the Cool Uses for Perl section of PerlMonks.

While Types::Standard provides all the type constraints Moose users will be familiar with (and a few more) there are other type libraries you can use instead of or as well as Types::Standard.


If your attribute or parameter needs to accept a file or directory name, I’d strongly recommend using Types::Path::Tiny. It provides Path, File, and Dir types, plus Abs* versions of them which coerce given filenames into absolute paths. The Path::Tiny objects it coerces strings into provide a bunch of helpful methods for manipulating files.

   package MyApp::Config {
      use Moo;
      use Types::Path::Tiny qw(AbsFile);
      use JSON::MaybeXS qw(decode_json);
      has config_file => (
         is      => 'ro',
         isa     => AbsFile->where(q{ $_->basename =~ q/\.json$/ }),
         coerce  => 1,
      sub get_hash {
         my $self = shift;
         decode_json( $self->config_file->slurp_utf8 );

Nice? Types::Path::Tiny is my personal favourite third-party type library. If you’re writing an application that needs to deal with files, use it.

Types::Common::String and Types::Common::Numeric

Types::Common::String provides a bunch of type constraints more specific than the standard Str type. If you have indicated that an attribute or parameter should be a string, it’s pretty rare that you really want to allow any string. You might want to constrain it more. This type library has types like NonEmptyStr and UpperCaseStr.

Types::Common::Numeric does the same for numbers, giving you type constraints like PositiveInt and IntRange[1,10].

Both of these libraries come bundled with Type::Tiny, so if you’re already using Types::Standard, won’t add any extra dependencies to your code.


This is a type library created for Type::Tiny’s internal use and gives you types like ArrayLike, HashLike, and CodeLike which allow overloaded objects.

Again it’s bundled with Type::Tiny, so won’t add any extra dependencies.


A type library for DateTime objects, allowing them to be coerced from strings.

   has start_date => (
      is      => 'ro',
      isa     => DateTimeUTC,
      coerce  => 1,

The above will not only coerce the attribute to a DateTime object, but coerce it to the correct timezone.

Comments are closed.