Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: Named parameters validation best practice

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

like image 315
Marcus Avatar asked Dec 09 '25 23:12

Marcus


1 Answers

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.

like image 198
Chris Avatar answered Dec 12 '25 15:12

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!