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";
};

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

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

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 parameter 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' }
}

This will make MooseX::Method create a method which takes two parameters, 'foo' and 'bar', of which only 'foo' is mandatory. 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.

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.

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 is to encapsulate the method declarations in a BEGIN block.

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 152:

Unknown directive: =over4

Around line 154:

'=item' outside of any '=over'

Around line 174:

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

Around line 183:

Unknown directive: =over4

Around line 185:

'=item' outside of any '=over'

Around line 188:

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

Around line 190:

Unknown directive: =over4

Around line 192:

'=item' outside of any '=over'

Around line 196:

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