++ed by:
ABRAXXA ADAMJS ANTIPASTA AWNCORP BOWTIE

32 PAUSE user(s)
18 non-PAUSE user(s).

Toby Inkster

NAME

Type::Tiny::Manual::UsingWithMouse - how to use Type::Tiny and Type::Library with Mouse

SYNOPSIS

   {
      package Person;
      
      use Mouse;
      use Types::Standard qw( Str Int );
      use Type::Utils qw( declare as where inline_as coerce from );
      
      has name => (
         is      => "ro",
         isa     => Str,
      );
      
      my $PositiveInt = declare
         as        Int,
         where     {  $_ > 0  },
         inline_as { "$_ =~ /^[0-9]\$/ and $_ > 0" };
      
      coerce $PositiveInt, from Int, q{ abs $_ };
      
      has age => (
         is      => "ro",
         isa     => $PositiveInt,
         coerce  => 1,
         writer  => "_set_age",
      );
      
      sub get_older {
         my $self = shift;
         my ($years) = @_;
         $PositiveInt->assert_valid($years);
         $self->_set_age($self->age + $years);
      }
   }

STATUS

Mouse support in Type::Tiny was somewhat of an afterthought. It should work, but is not anywhere near as well-tested as Moo or Moose support.

DESCRIPTION

Type::Tiny type constraints have an API almost identical to that of Mouse::Meta::TypeConstraint. As a result, you can use a Type::Tiny object pretty much anywhere you'd use a Mouse::Meta::TypeConstraint and you are unlikely to notice the difference. (And Mouse is unlikely to notice the difference too!)

Per-Attribute Coercions

Type::Tiny offers convenience methods to alter the list of coercions associated with a type constraint. Let's imagine we wish to allow our name attribute to be coerced from an arrayref of strings.

      has name => (
         is      => "ro",
         isa     => Str->plus_coercions(
            ArrayRef[Str], sub { join " ", @{$_} },
         ),
         coerce  => 1,
      );

This coercion will apply to the name attribute only; other attributes using the Str type constraint will be unaffected.

See the documentation for plus_coercions, minus_coercions and no_coercions in Type::Tiny.

Optimization

Mouse's built-in type constraints are implemented using XS and are stupidly fast. Using Type::Tiny type constraints will be significantly slower. However, Type::Tiny constraints should not be significantly slower (and may even be faster) than non-built-in Mouse type constraints.

SEE ALSO

For examples using Type::Tiny with Mouse see the SYNOPSIS sections of Type::Tiny and Type::Library, and the files mouse.t and mouse-coercion.t in the Type-Tiny test suite.

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.




Hosting generously
sponsored by Bytemark