I am using named parameters in class method calls and was wondering if there is a best practice to make sure no unknown parameters are passed. Here's what I am doing
sub classmethod {
my $self = shift;
my %args = (
"param1" => "default1",
"param2" => "default2",
@_
)
if (my @invalid = grep { !/^(param1|param2)$/ } keys %args) {
croak "received unknown arg(s) ".join(",", @invalid)." from ".caller();
}
}
Is that a proper way to go forward, or could this cause a performance problem?
Best, Marcus
You could use Params::Validate. Another option is Params::Check
If params are fixed, then its best to validate them during development, with the option to turn off validation when live.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With