为什么我的Perl程序在参数值为0时打印帮助消息?
如果我这样做:为什么我的Perl程序在参数值为0时打印帮助消息?,perl,parameters,integer,zero,getopt-long,Perl,Parameters,Integer,Zero,Getopt Long,如果我这样做: GetOptions( 'u=s' => \$in_username, 'r=i' => \$in_readonly, 'b=i' => \$in_backup ); exit usage() unless $in_username && $in_readonly && $in_backup; 然后像这样调用程序: ./app.pl -u david -r 12 -b 0 它总是导致
GetOptions(
'u=s' => \$in_username,
'r=i' => \$in_readonly,
'b=i' => \$in_backup
);
exit usage() unless $in_username && $in_readonly && $in_backup;
然后像这样调用程序:
./app.pl -u david -r 12 -b 0
它总是导致调用usage(),因此0显然不被视为整数值。
接受整数值和0怎么办?当作为布尔值处理时,Perl认为0是假值 你需要像这样的东西
exit usage() unless defined($in_username) && defined($in_readonly) && defined(in_backup);
编辑
请参阅msw对原始问题的精彩评论您的陈述“0不被视为整数值”是正确的,但“0”被解释为错误:“如果标量值不是空字符串或数字0(或其等效字符串“0”),则在布尔意义上被解释为真”。布尔上下文只是一种特殊的标量上下文,其中从未执行到字符串或数字的转换。”Dancrumb的回答是正确的,尽管没有明确说明您的错误陈述。