Perl 带有标量条件的If语句

Perl 带有标量条件的If语句,perl,Perl,我很难理解Perl中的以下表达式 for (my $attempts = 0; $attempts < $maxatt; $attempts++) { print "Please try again.\n" if $attempts; print "$prompt: "; $response = readline(*STDIN); chomp($response); r

我很难理解
Perl
中的以下表达式

 for (my $attempts = 0; $attempts < $maxatt; $attempts++) {
            print "Please try again.\n" if $attempts;
            print "$prompt: ";
            $response = readline(*STDIN);
            chomp($response);
            return $response if $response;
    }
for(my$attempts=0;$attempts<$maxatt;$attempts++){
如果$尝试,则打印“请重试。\n”;
打印“$prompt:”;
$response=readline(*STDIN);
chomp($response);
如果$response,则返回$response;
}

使用标量作为条件是什么意思?也就是说,是什么让它返回真与假?我假设语句读作
if($attempts){print…}

Perl没有“布尔值”的明确概念

Per:

如果标量值未定义,空字符串或数字0(或其等效字符串“0”)在布尔意义上解释为FALSE,如果是其他值,则解释为TRUE。布尔上下文只是一种特殊的标量上下文,在这种上下文中,不会执行到字符串或数字的转换

因此,在您的示例中,
if$attempts
表示
if$attempts>0

顺便说一句,这也意味着像
$attempts>0
这样的东西不会计算为布尔值,因为没有这样的东西。相反,根据,像
这样的运算符:

[…]为true返回
1
,并返回定义的空字符串的特殊版本,
“”
,该字符串计为零,但不受有关不正确数字转换的警告的影响,就像
“0但为true”
一样

(帽子尖到,指出它们返回的FALSE不是正常的空字符串。)