Perl:命名参数验证最佳实践 [英] Perl: Named parameters validation best practice

查看:90
本文介绍了Perl:命名参数验证最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类方法调用中使用命名参数,并且想知道是否有最佳实践来确保不传递任何未知参数.这就是我在做什么

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?

最好, 马库斯

推荐答案

您可以使用 Params :: Check

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.

这篇关于Perl:命名参数验证最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆