NAME

MooseX::Method - Method declaration with type checking

SYNOPSIS

package Foo;

use Moose;
use MooseX::Method;

method hello => {
  who => { isa => 'Str',required => 1 },
  age => { isa => 'Int',required => 1 },
} => sub {
  my ($self,$args) = @_;

  print "Hello $args->{who}, I am $args->{age} years old!\n";
};

method morning => [
  { isa => 'Str',required => 1 },
] => sub {
  my ($self,$name) = @_;

  print "Good morning $name!\n";
};

Foo->hello (who => 'world',age => 42); # This works.

Foo->morning ('Jens'); # This too.

Foo->hello (who => 'world',age => 'fortytwo'); # This doesn't.

Foo->morning; # This neither.

DESCRIPTION

The problem

This module is an attempt to solve a problem I've often encountered but never really found any good solution for, namely validation of method parameters. How many times haven't we all found ourselves writing code like this:

sub foo {
  my ($self,$args) = @_;

  die "Invalid arg1"
    unless (defined $arg->{bar} && $arg->{bar} =~ m/bar/);
}

Manual parameter validation is a tedious and repetive process and maintaining it consistently throughout your code can be downright hard sometimes. Modules like Params::Validate makes the job a bit easier but it doesn't do much for elegance and it still requires more weird code than what should strictly speaking be neccesary.

The solution

MooseX::Method to the rescue. It lets you declare what parameters people should be passing to your method using Moose-style declaration and Moose types. It doesn't get much Moosier than this.

DECLARING METHODS

method $name => {} => sub {}

The exported function method installs a method into the class from which it is called from. The first parameter it takes is the name of the method. The second parameter is a signature declaration, for more information on that, see below. The third parameter is a coderef that should be run when the method is called assuming that all parameters satisfies the requirements of the parameter specifications.

Parameter specifications

The method specification should look something to this effect:

{
  foo => { isa => 'Int',required => 1 },
  bar => { isa => 'Int' }
}

Or for positional arguments...

[
  { isa => 'Int',required => 1 },
  { isa => 'Int' },
]

The first example will make MooseX::Method create a method which takes two parameters, 'foo' and 'bar', of which only 'foo' is mandatory. The second example will create two positional parameters with the same properties.

Currently, the specification for a parameter may set any of the following fields:

isa

If a value is provided, it must satisfy the constraints of the type specified in this field.

does

Require that the value provided is able to do a certain role.

default

Sets the parameter to a default value if the user does not provide it.

required

If this field is set, supplying a value to the method isn't optional but the value may be supplied by the default field.

coerce

If the type supports coercion, attempt to coerce the value provided if it does not satisfy the requirements of isa. See Moose for examples of how to coerce.

metaclass

This is used as parameter metaclass if specified. If you don't know what this means, read the documentation for Moose.

name

If this is provided, it overrides the name of the parameter when errors are reported. This gets filled in automagically by MooseX::Method if you don't specify it, even if you use a custom metaclass.

CAVEATS

Methods are added to the class at runtime, which obviously means they won't be available to play with at compile-time. Moose won't mind this but a few other modules probably will. A workaround for this that sometimes work is to encapsulate the method declarations in a BEGIN block.

One of these modules is Devel::Cover. If anyone have a fix for this, it would be very much appreciated.

ACKNOWLEDGEMENTS

Stevan Little for making Moose and luring me into the world of metafoo.

SEE ALSO

Moose
The #moose channel on irc.perl.org

BUGS

Most software has bugs. This module probably isn't an exception. If you find a bug please either email me, or add the bug to cpan-RT.

AUTHOR

Anders Nor Berle <debolaz@gmail.com>

COPYRIGHT AND LICENSE

Copyright 2007 by Anders Nor Berle.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

9 POD Errors

The following errors were encountered while parsing the POD:

Around line 172:

Unknown directive: =over4

Around line 174:

'=item' outside of any '=over'

Around line 209:

You forgot a '=back' before '=head1'

Around line 222:

Unknown directive: =over4

Around line 224:

'=item' outside of any '=over'

Around line 227:

You forgot a '=back' before '=head1'

Around line 229:

Unknown directive: =over4

Around line 231:

'=item' outside of any '=over'

Around line 235:

You forgot a '=back' before '=head1'