加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl – 检查错误哈希键输入的最佳方法

发布时间:2020-12-16 06:23:16 所属栏目:大数据 来源:网络整理
导读:在我的Perl脚本中,我有一个子程序被调用了数百次,包含许多不同的参数集,因为发送的唯一值是与默认值不同的值. (不言而喻,排列和组合的数量非常大)为了使其更加健壮,我想对参数进行一些检查.这是我的子程序的缩小版本(实际版本有许多参数,具有非常具体,有时冗
在我的Perl脚本中,我有一个子程序被调用了数百次,包含许多不同的参数集,因为发送的唯一值是与默认值不同的值. (不言而喻,排列和组合的数量非常大)为了使其更加健壮,我想对参数进行一些检查.这是我的子程序的缩小版本(实际版本有许多参数,具有非常具体,有时冗长的名称):

# Obtain any parameters that differ from the defaults and send for processing
sub importantSub
{
   my %params = 
   (
      commandType       => 5,commandId         => 38,channel1Enable    => 0,channel2Enable    => 0,channel3Enable    => 0,channel4Enable    => 0,channel5Enable    => 0,channel6Enable    => 0,channel7Enable    => 0,channel8Enable    => 0,channel9Enable    => 0,channel10Enable   => 0,# This goes on for a VERY long time
      @_
   );

    # Make sure we have exactly as many keys as we expect - verify that
    # no additional parameters were added (Real version has 92)
   if( keys(%params) !=  92 ) 
   {
      croak("Unexpected parameter in hash!");
   }

   return &$privateProcessingFunction('Data Path Configuration',%params);
}

正如您所看到的,我目前正在检查值的数量是否相同,就像是以“chan1Enable”而不是“channel1Enable”发送某些内容一样,它会抛弃该数字.

但是,由于多个其他工程师编写的多个其他脚本对子程序的调用如此之多,我想找到一种方法来查找WHICH值是不正确的(例如,不要只说有一个意外的参数,说“chan1Enable”无效).此外,如果多个值不正确,我想列出所有这些值.

最有效的方法是什么?

(我询问效率,因为当前以400多种不同的方式调用函数,随着应用程序的扩展,这种方式可能会继续增长.)

解决方法

有两种错误:提供无法识别的参数,或者未提供已识别的参数.在编辑参数列表时,您必须担心第二个问题,并确保在整个应用程序中始终如一地使用新参数.

最好和最简单的解决方案是使用另一个哈希.

my @params = qw(commandType commandId channel1Enabled ...);
my %copy = %params;
my @validation_errors = ();

# are all the required parameters present?
foreach my $param (@params) {
    if (not exists $copy{$param}) {
        push @validation_errors,"Required param '$param' is missing.";
    }
    delete $copy{$param};
}

# since we have  delete'd  all the recognized parameters,# anything left is unrecognized
foreach my $param (keys %copy) {
    push @validation_errors,"Unrecognized param '$param' = '$copy{$param}' in input.";
}

if (@validation_errors) {
    die "errors in input:n",join("n",@validation_errors);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读