use strict; use warnings; { use Moose::Util::TypeConstraints; BEGIN { subtype 'PosInt', as 'Int', where { $_ > 0 }, message {"Must be a positive int"}; subtype 'Pair', as 'ArrayRef', where { @{$_} == 2 }, message {"Must be a two-elem array"}; } } { package Foo; use Moose; use MooseX::Params::Validate qw( validated_list ); sub requires_str { my ( $self, $str ) = validated_list( \@_, str => { isa => 'Str' }, ); 1; } sub requires_pos_int { my ( $self, $int ) = validated_list( \@_, int => { isa => 'PosInt' }, ); 1; } sub requires_several { my ( $self, $str, $int, $pair ) = validated_list( \@_, str => { isa => 'Str' }, int => { isa => 'PosInt' }, pair => { isa => 'Pair' }, ); 1; } no Moose; __PACKAGE__->meta()->make_immutable(); } { package Bar; use Moose; use MooseX::Method::Signatures; method requires_str (Str :$str!) { 1; } method requires_pos_int (PosInt :$int!) { 1; } method requires_several (Str :$str!, PosInt :$int!, Pair :$pair!) { 1; } no Moose; __PACKAGE__->meta()->make_immutable(); } my $foo = Foo->new(); sub mxpv_success { eval { $foo->requires_str( str => 'string' ) }; eval { $foo->requires_pos_int( int => 42 ) }; eval { $foo->requires_several( str => 'string', int => 42, pair => [ 0, 1 ], ); }; } sub mxpv_failure { eval { $foo->requires_str( str => undef ) }; eval { $foo->requires_pos_int( int => -1 ) }; eval { $foo->requires_several( str => 'string', int => 42, pair => [1], ); }; eval { $foo->requires_several( str => 'string', int => 'nine', pair => [ 1, 2 ], ); }; } my $bar = Bar->new(); sub mxd_success { eval { $bar->requires_str( str => 'string' ) }; eval { $bar->requires_pos_int( int => 42 ) }; eval { $bar->requires_several( str => 'string', int => 42, pair => [ 0, 1 ], ); }; } sub mxd_failure { eval { $bar->requires_str( str => undef ) }; eval { $bar->requires_pos_int( int => -1 ) }; eval { $bar->requires_several( str => 'string', int => 42, pair => [1], ); }; eval { $bar->requires_several( str => 'string', int => 'nine', pair => [ 1, 2 ], ); }; } use Benchmark qw( cmpthese ); my $count = shift || 10_000; cmpthese( $count, { 'MXPV success' => \&mxpv_success, 'MXPV failure' => \&mxpv_failure, 'MXMS success' => \&mxd_success, 'MXMS failure' => \&mxd_failure, }, );