Perl && 的结果是什么?

发布于 2024-09-28 12:10:55 字数 108 浏览 0 评论 0原文

当我尝试这个时:

$a = 1;
$b = 2;
print ($a && $b) . "\n";

结果是2。为什么?

When I try this:

$a = 1;
$b = 2;
print ($a && $b) . "\n";

The result is 2. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初心 2024-10-05 12:10:55

引用 perlop

“||”、“//”和“&&”运营商
返回最后评估的值
(与 C 的“||”和“&&”不同,
返回 0 或 1)。

Perl 认为生成的 2 为 true,因此当您在逻辑条件下使用 && 运算符时,一切都会按预期工作。额外的好处是,您也可以在其他上下文中使用逻辑运算符:

sub say_something {
    say shift || 'default';
}

say_something('foo'); # prints 'foo'
say_something(); # prints 'default'

甚至作为流程修饰符:

my $param = shift || die "Need param!";
-f $file && say "File exists.";

在最后两个示例中,很高兴认识到如果 && 它们无法工作和 || 运算符没有短路。如果您在第一行shift一个真值,则没有必要评估右侧(die…),因为整个表达式无论如何都是true。如果文件测试在第二行失败,则不需要再次评估右侧,因为总体结果是错误的。如果逻辑运算符无论如何都坚持评估整个表达式,我们就不能以这种方式使用它们。

Quote perlop:

The "||", "//" and "&&" operators
return the last value evaluated
(unlike C's "||" and "&&", which
return 0 or 1).

The resulting 2 is considered true by Perl, so that when you use the && operator in a logical condition, everything works as expected. The added bonus is that you can use the logical operators in other contexts as well:

sub say_something {
    say shift || 'default';
}

say_something('foo'); # prints 'foo'
say_something(); # prints 'default'

Or even as flow modifiers:

my $param = shift || die "Need param!";
-f $file && say "File exists.";

In the last two examples it’s good to realize that they could not work if the && and || operators did not short-circuit. If you shift a true value in on first line, there is no point evaluating the right side (die…), since the whole expression is true anyway. And if the file test fails on the second line, you don’t need to evaluate the right side again, since the overall result is false. If the logical operators insisted on evaluating the whole expression anyway, we could not use them this way.

尾戒 2024-10-05 12:10:55

这就是 && 运算符的工作原理:如果左侧参数的计算结果为 true,则表达式的值就是右侧参数的值。 perlop 页面对此进行了介绍。

然而,你也让自己暴露了一个更微妙的问题。您会发现换行符没有被打印。这是因为,如果您将表达式放在 print (或任何其他函数名称)后面的括号中,则传递给 print 的参数就是括号中的参数。要注意到这一点,请确保打开警告。将这些行放在每个程序的顶部:

#!/usr/bin/perl -w

use strict;

直到您足够理解它们并自行决定是否继续它们。 :-)

在你的情况下,你会得到这个:

print (...) interpreted as function at p line 7.
Useless use of concatenation (.) or string in void context at p line 7.

That's how the && operator works: if the left-hand argument evaluates as true, the value of the expression is that of the value of the right-hand argument. This is covered on the perlop page.

However, you've also let yourself open to a much more subtle problem. You'll find that the newline doesn't get printed. This is because if you put an expression in brackets after print (or any other function name) the arguments passed to print are just those in the brackets. To get notice of this, make sure you switch on warnings. Put these lines at the top of each program:

#!/usr/bin/perl -w

use strict;

until you understand them enough to decide for yourself whether to continue with them. :-)

In your case, you'll get this:

print (...) interpreted as function at p line 7.
Useless use of concatenation (.) or string in void context at p line 7.
沫尐诺 2024-10-05 12:10:55

因为 && 运算符计算右操作数,并在左操作数计算结果为 true 时返回结果。

Because the && operator evaluates the right operand and returns the result when the left operand evaluates to true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文