gpt4 book ai didi

Perl OR 条件

转载 作者:行者123 更新时间:2023-12-04 22:39:11 25 4
gpt4 key购买 nike

我有一个包含许多这样的 OR 条件的代码(工作正常)来检查其中一个值是否为空,然后我们抛出一条错误消息(所有这些都必须填写)

 } elsif (
!$params{'account'}
|| !$params{'type'}
|| !$params{'sampledate'}
|| !$params{'lastpwchange'}
|| !$params{'system'}
|| !$params{'wspass'}
) {
print $cgi->header("text/plain"), "some mandatory parameters are missing";
}

我需要在这种情况下添加许多其他变量,使该行有点不可读。

我想知道是否有更优雅的方式来指定所有这些,也许使用某种数组/列表?

谢谢你

最佳答案

使用 handy module List::Util ,您可以执行以下操作:

use List::Util qw/any/;

...
elsif (any { !$params{$_} } qw/account type sampledate lastpwchange system wspass/) {
print $cgi->header("text/plain"), "some mandatory parameters are missing";
}

请注意,您的表达式并不完全检查是否为空;它实际上执行 bool 检查,因此参数值为 0也将无法通过检查。这可能是,也可能不是您想要的。

关于Perl OR 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58834446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com