将零传递给 Getopt::Std
我在 Perl 脚本中使用 Getopt::Std,并且希望传入零作为值。我正在使用 unless()
检查值是否设置正确。目前 unless()
拒绝该值,因为该值未设置。
有没有办法让 unless()
接受零作为有效值(任何非负整数都是有效的)。
这可能非常简单,但几天前我从未接触过 Perl!
富有的
I am using Getopt::Std
in a Perl script, and would like to pass in a zero as value. I am checking that values are set correctly using unless()
. At the moment unless()
is rejecting the value as being unset.
Is there a way to get unless()
to accept zero as a valid value (any non-negative integer is valid).
This is probably perfeclty simple, but I've never touched Perl before a few days ago!
Rich
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Perl 5 有几个错误值:
0
、"0"
、""
、undef
、()
。值得注意的是,有些事情可能看起来应该是错误的,但事实并非如此。例如
0.0
为 false,因为它是相当于0
的数字,但"0.0"
不是(唯一为 false 的字符串是空字符串 (""
) 和"0"
)。它还具有定义性的概念。分配有值(除了 undef 之外)的变量被认为是已定义的,并且在使用
定义
函数。鉴于您希望参数是非负整数,最好进行测试:
Perl 5 has several false values:
0
,"0"
,""
,undef
,()
.It is important to note that some things may look like they should be false, but aren't. For instance
0.0
is false because it is number that is equivalent to0
, but"0.0"
is not (the only strings which are false are the empty string (""
) and"0"
).It also has the concept of definedness. A variable that has a value (other than undef) assigned to it is said to be defined and will return true when tested with the
defined
function.Given that you want an argument to be a non-negative integer, it is probably better to test for that:
您需要使用
unless Defined
而不是unless
,因为 Perl 中零为 false。You need to use
unless defined <SOMETHING>
instead ofunless <SOMETHING>
, because zero is false in Perl.