August 2018 Archives

Exploring Type::Tiny Part 5: match_on_type

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 fifth 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.

It's pretty common to do things like this:

   use Types::Standard qw( is_ArrayRef is_HashRef );
   use Carp qw( croak );
   
   sub process_data {
      my ($self, $data) = @_;
      if (is_ArrayRef($data)) {
         $self->_process_value($_) for @$data;
      }
      elsif (is_HashRef($data)) {
         $self->_process_value($_) for values %$data;
      }
      else {
         croak "Could not grok data";
      }
   }

Type::Utils provides a perhaps slightly neater way to do this:

Exploring Type::Tiny Part 4: Using Types::Standard as a Ref::Util-Like Library

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 third in a series of posts showing other things you can use Type::Tiny for. This article along with part 1, part 2, and part 3 can be found on my blog and in the Cool Uses for Perl section of PerlMonks.

Even if you read the documentation of Types::Standard pretty thoroughly, you'd probably miss that you can do things like this:

   use Types::Standard qw(is_ArrayRef is_HashRef);
   
   if (is_ArrayRef($var)) {
      ...;
   }
   elsif (is_HashRef($var)) {
      ...;
   }

It is documented that Types::Standard exports functions called ArrayRef and HashRef, which are constant-like functions returning Moose/Moo-compatible type constraint objects, but where did these is_ArrayRef and is_HashRef functions come from?

Exploring Type::Tiny Part 3: Using Type::Tie

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 third in a series of posts showing other things you can use Type::Tiny for. This article along with part 1 and part 2 can be found on my blog and in the Cool Uses for Perl section of PerlMonks.

This works:

   use Types::Standard qw(Int);
   
   tie(my @numbers, Int);
   
   push @numbers, 1, 2, 3;     # ok
   push @numbers, "four";      # dies

Well, if you try it, you may find it complains about not being able to load Type::Tie.

Type::Tie is an add-on for Type::Tiny distributed separately. It's an optional dependency, so if you want to use this feature, you'll need to make sure it's installed.

Exploring Type::Tiny Part 2: Using Type::Tiny with Moose

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 second in a series of posts showing other things you can use Type::Tiny for. You can refer back to part 1.

Type::Tiny is often used in Moo classes and roles as a drop-in replacement for Moose's built-in type system. But the original reason I wrote it was as a response to the growing number of MooseX::Types and MouseX::Types modules on CPAN. I thought "wouldn't it be good if you could write a type library once, and use it for Moose, Mouse, and maybe even Moo?" In the very early version, you needed to import types like this:

   use Type::Standard -moose, qw(Int);
   use Type::Standard -mouse, qw(Int);
   use Type::Standard -moo,   qw(Int);

Specifying which object system you were using allowed the type library to export different blessed type constraint objects for different object frameworks. Eventually this need was eliminated by having Type::Tiny's objects better mock the Moose and Mouse native APIs, so the frameworks didn't even notice you weren't using their built-in type constraints.

(While no longer documented, the -moose, etc import flags still work in all Type::Library-based type libraries.)

Anyway, so now you know Type::Tiny types can work with Moose, what are the reasons to use them over Moose's built-in type constraints?

About Toby Inkster

user-pic I'm tobyink on CPAN, IRC and PerlMonks.