Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的Perl程序在参数值为0时打印帮助消息?_Perl_Parameters_Integer_Zero_Getopt Long - Fatal编程技术网

为什么我的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的回答是正确的,尽管没有明确说明您的错误陈述。