views:

262

answers:

2

I'm looking for a general-purpose module to take the drudgery out of validating subroutine and method arguments. I've scanned through various possibilities on CPAN: Params::Validate, Params::Smart, Getargs::Mixed, Getargs::Long, and a few others.

Any information regarding pros and cons of these or other modules would be appreciated. Thanks.

+6  A: 

If you start to use Moose, you'll find MooseX::Types to your liking. Types automatically have an is_$type(), and to_$type(). These are for making sure you input passes type constraints, or making your input has a valid coercion to the type. I like them better even for these types of things because you can ensure the state of your object has the said types at no additional cost.

use Moose;
has 'foo' => ( isa => MyType, is => ro );

sub _check_my_type {
  my ( $self, $type ) = @_;
  is_MyType( $type );
};

It might be lacking some support for deep/recursive types, but if your using this stuff in modern perl you're probably "doing it wrong." Instead use an object that has its own consistency checks (like mine above with MyType), and just pass the object.

Evan Carroll
Moose isn't a bad suggestion if you can use it. If you can't, Mouse is Moose-light. This will get you the type checking and a few other things without all of the dependencies of Moose and with out the same compile time hit.To be clear, I <3 Moose, but for CGIs and such it isn't always the best choice.
mikegrb
Mouse isn't being actively developed. It is a dead project (per the docs of Mouse.pm Use Moose instead of Mouse.). Unfortunately, Mouse docs even don't suggest it for CGI! Instead, they say "Though significant progress has been made over the years, the compile time penalty is a non-starter for some very specific applications. If you are writing a command-line application or CGI script where startup time is essential, you may not be able to use Moose. We recommend that you instead use HTTP::Engine and FastCGI for the latter, if possible."http://rt.cpan.org/Public/Bug/Display.html?id=42203
Evan Carroll
+5  A: 

Have a look at MooseX::Method::Signatures which provides a bit more than just validating the arguments.

Example from POD:

package Foo;

use Moose;
use MooseX::Method::Signatures;

method morning (Str $name) {
    $self->say("Good morning ${name}!");
}

method hello (Str :$who, Int :$age where { $_ > 0 }) {
    $self->say("Hello ${who}, I am ${age} years old!");
}

method greet (Str $name, Bool :$excited = 0) {
    if ($excited) {
        $self->say("GREETINGS ${name}!");
    }
    else {
        $self->say("Hi ${name}!");
    }
}

MooseX::Method::Signatures also comes as standard with MooseX::Declare which brings even more sexy syntax to the Perl plate. The above could be written like so (just showing first method for brevity):

use MooseX::Declare;

class Foo {

    method morning (Str $name) {
        $self->say("Good morning ${name}!");
    }
}

There is also a corollary signatures CPAN module for plain subroutines but unfortunately it isn't as feature rich as above.

/I3az/

draegtun