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

NAME

Type::Tiny::Manual::Params - coerce and validate arguments to functions and methods

DESCRIPTION

There is a module called Type::Params available to wrap up type coercion and constraint checks into a single, simple and fast check. If you care about speed, and your sub signatures are fairly simple, then this is the way to go...

   use feature qw( state );
   use Types::Standard qw( Str );
   use Type::Utils;
   use Type::Params qw( compile );
   
   my $Invocant = class_type { class => __PACKAGE__ };
   
   sub set_name
   {
      state $check = compile($Invocant, Str);
      my ($self, $name) = $check->(@_);
      
      ...;
   }

See the COOKBOOK section of Type::Params for further information.

The Somewhat More Manual Way...

In general, Type::Params should be sufficient to cover most needs, and will probably run faster than almost anything you could cook up yourself. However, sometimes you need to deal with unusual function signatures that it does not support. For example, imagine function format_string takes an optional hashref of formatting instructions, followed by a required string. You might expect to be able to handle it like this:

   sub format_string
   {
      state $check = compile(Optional[HashRef], Str);
      my ($instructions, $string) = $check->(@_);
      
      ...;
   }

However, this won't work, as Type::Params expects required parameters to always precede optional ones. So there are times you need to handle parameters more manually.

In these cases, bear in mind that for any type constraint object you have several useful checking methods available:

  Str->check($var)            # returns a boolean
  is_Str($var)                # ditto
  Str->($var)                 # returns $var or dies
  assert_Str($var)            # ditto

Here's how you might handle the format_string function:

   sub format_string
   {
      my $instructions;
      $instructions = shift if HashRef->check($_[0]);
      
      my $string = Str->(shift);
      
      ...;
   }

Alternatively, you could manipulate @_ before passing it to the compiled check:

   sub format_string
   {
      state $check = compile(HashRef, Str);
      my ($instructions, $str) = $check->(@_==1 ? ({}, @_) : @_);
      
      ...;
   }

Signatures

Don't you wish your subs could look like this?

   sub set_name (Object $self, Str $name)
   {
      $self->{name} = $name;
   }

Well; here are a few solutions for sub signatures that work with Type::Tiny...

Attribute::Contract

You want Attribute::Contract 0.03 or above.

   use Attribute::Contract -types => [qw/Object Str/];
   
   sub set_name :ContractRequires(Object, Str)
   {
      my ($self, $name) = @_;
      $self->{name} = $name;
   }

Attribute::Contract also includes support for type checking of the returned value.

Function::Parameters

The following should work from Function::Parameters 1.0101:

   use Function::Parameters qw(:strict);
   use Types::Standard;
   
   fun set_name ((Object) $self, (Str) $name)
   {
      $self->{name} = $name;
   }
   
   # or...
   method set_name ((Str) $name)
   {
      $self->{name} = $name;
   }

In Function::Parameters 1.0101 and 1.0102, Moose was required to be loaded for this to work (even though it's not actually being used for type checks). Since Function::Parameters 1.0103, Moose is no longer required.

Note the parentheses around the type names are required, because without parentheses, Function::Parameters will use Moose type constraints:

   method set_name (Str $name)  # Moose type constraint
   {
      $self->{name} = $name;
   }

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.