Params::Validate has too many features, (I know that sounds odd, but read on). It is also slow.
Yah. In trying to solve the generalized problem of parameter validation (which makes it darn useful - but only in specialized situations), it makes the simple cases complex. This is precisely the problem that my idea of adding some syntactic sugar proposes to address.
use Params::Validate qw (validate_with); sub example_method { my $self = shift; my %args = validate_with ( params => \@_, spec => { thing => 1, handle => 1 }, normalize_keys => sub { return lc($_[0]); }, ); my ($handle, $thing) = @args{'handle','thing'}; #... }
Uh huh. How long did it take you to figure out exactly what it was doing? That does the same thing as:
use Class::ParmList qw (simple_parms); sub example_method { my $self = shift; my ($handle, $thing) = simple_parms(['handle','thing'],@_); #... }
Except Params::Validate does it half as fast and considerably less clearly.
Ideally, I want Sub::Parms to do be able to do something like this:
use Sub::Parms; sub example_method { Invokation : $self; NamedParam : $handle (+handle); NamedParam : $thing (+thing); #... }
and have it run at the same speed as this (which is about 3x faster than Params::Validate and almost 2x faster than Class::ParmList is while doing exactly the same thing):
sub example_method { my $self = shift; my %args; { my %raw_args = @_; %args = map {lc($_) => $raw_args{$_}} keys %raw_args; } unless ($args{'handle'}) { croak("Missing 'handle' argument"); } unless ($args{'thing'}) { croak("Missing 'thing' argument"); } unless (4 == @_) { croak("Unexpected extra parameters"); } my ($handle,$thing) = @args{'handle','thing'}; #... }
The idea is that the simple cases should be simple and clear.
In reply to Re^2: RFC named parameter syntactic sugar
by snowhare
in thread RFC named parameter syntactic sugar
by snowhare
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |