Perl 中的列表运算符优先级

发布于 2024-09-13 14:47:29 字数 694 浏览 3 评论 0原文

我正在读《Beginning Perl》一书,它给出了这两个语句:

print "Test one: ", 6 > 3 && 3 > 4, "\n";
print "Test two: ", 6 > 3 and 3 > 4, "\n";

第一行不打印任何内容,但换行,第二行打印 1,不换行。

我对输出感到困惑。根据作者的说法,第二个语句给出了奇怪的输出,因为它就像在说:

print ("Test two: ", 6 > 3) and 3 > 4, "\n";

但是,为什么第一个语句不一样?我认为这与印刷的优先顺序有关。 &&比 print 具有更高的优先级,因此首先对其进行评估,然后再进行打印。而“and”的优先级低于 print,因此 6 > > 3 将被打印,打印返回 1,然后用“and”对其进行计算。然而这并没有什么意义。

我已经阅读了有关列表运算符的优先级如何工作的 Perl 文档,但我仍然不明白这个示例。你们能剖析一下这两个语句,并告诉我首先打印的是什么吗?您能否解释一下 Perl 文档中提到列表运算符“向左”和“向右”时的含义?谢谢。


非常感谢大家的回答。我现在明白了。我确实按照cjm所说的做了,并认为有向左和向右列表运算符。现在我明白了这意味着什么,我就明白了整件事。

I'm reading the "Beginning Perl" book, and it gives these two statements:

print "Test one: ", 6 > 3 && 3 > 4, "\n";
print "Test two: ", 6 > 3 and 3 > 4, "\n";

The first line prints nothing with a new line, the second line prints a 1 with no new line.

I'm confused about the output. According to the author, the second statement gives weird output because it's just like saying:

print ("Test two: ", 6 > 3) and 3 > 4, "\n";

However, why is the first statement not the same? I thought it had something to do with print's precedence. The && has higher precedence than print, so that's evaluated first and then it's printed. Whereas the "and" has lower precedence than print, so 6 > 3 will be printed, print returns a 1, and then that is evaluated with the "and." However this doesn't really make sense.

I've read the Perl documentation on how precedence works for list operators, but I still do not understand this example. Can you guys dissect the two statements, and tell me what is printed first? Can you also explain what the Perl documentation means when it mentions list operators as being "leftward" and "rightward?" Thanks.


Thanks a lot everyone for your answers. I understand it now. I was indeed doing what cjm said, and thinking that there are leftward and rightward list operators. So now that I understand what it means, I understand the whole thing.

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

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

发布评论

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

评论(3

屌丝范 2024-09-20 14:47:29

好的,对于初学者来说:列表运算符是 Perl 中优先级最低的事物之一,但仅在其右侧。你问这意味着什么:好吧,让我们简单一点。假设有一个名为 foo 的列表。它做什么并不重要,但它就在那里。您可以使用 sub foo { map 10 * $_, @_ } 轻松创建这样的东西,它返回每个参数乘以 10 的结果。顺便说一下:

print 1, 2, 3; 相当于 print( 1, 2, 3 );
print foo 1, 2, 3; 相当于 print( foo( 1, 2, 3 ) );
print 1, foo 2, 3; 等价于 print( 1, foo( 2, 3 ) );

我们可以看到 foo 吞噬了在右侧尽可能多地向上 - 只有语句的结尾(到目前为止......)可以阻止它。如果我们写成 @array = (1, foo 2, 3); ,那么它就相当于 @array = (1, foo(2, 3) ); 因为当然以括号结尾的内容仍然适用。

由于逗号的优先级也非常低(就在“列表运算符(向右)”上方),我们还可以将几乎任何类型的表达式放入 listop 的参数中 - 这只是 perl 确保我们不这样做的方式大多数时候不必添加括号。数学、按位运算符、比较,甚至正则表达式匹配都具有更高的优先级。

唯一具有较低优先级的事情是拼写出来的逻辑连接词 andorxor、和。因此,如果我们

foo 1, 2, 3 and 4;

这样写,则意味着 (foo(1, 2, 3) and 4) —— foo 的参数停在 and.在一个人为的例子中这似乎很愚蠢,所以让我们把它变成一个常见的 Perl 习惯用法:

open $fh, '<', $filename or die "$! opening $filename";

它相当于

(open($fh, '<', $filename) or die("$! opening $filename"));

使用

die "$! opening $filename" unless open $fh, '<', $filename;

语句修饰符形式 unless (其中根本不是一个运算符,每个语句只允许一次,并且只出现在语句末尾,因此它根本不参与优先级,但您可以将其视为向左“低于最低”优先级)。

所以无论如何,返回到您的原始代码示例 - print 的参数在 and 的左侧以及 and 的右侧结束是一个完全独立的逗号表达式 - 它什么也不做,因为它只是一些常量 3 >; 4"\n" 在 void 上下文中求值。

Okay, for starters: list-operators are among the lowest-precedence things in perl, but only on their right side. You ask what that means: well, let's make it simple. Suppose that there is a listop called foo. It doesn't matter what it does, but it's there. You can easily create such a thing with sub foo { map 10 * $_, @_ } which returns each of its arguments multiplied by ten. That out of the way:

print 1, 2, 3; is equivalent to print( 1, 2, 3 );
print foo 1, 2, 3; is equivalent to print( foo( 1, 2, 3 ) );
print 1, foo 2, 3; is equivalent to print( 1, foo( 2, 3 ) );

We can see that foo gobbles up as much as it can on the right side — only the end of the statement (so far...) can stop it. If we wrote @array = (1, foo 2, 3); that would be equivalent to @array = (1, foo(2, 3) ); because of course ending surrounding parentheses still applies.

Since commas are also very low priority (just above "List operators (rightward)"), we can also put pretty much any kind of expression we want into the arguments to a listop — this is just perl's way of making sure that we don't have to add parentheses most of the time. Math, bitwise operators, comparisons, even regex matches have higher priority.

The only things that do have lower-priority are the spelled-out logical connectives and, or, xor, and not. So if we write

foo 1, 2, 3 and 4;

that means (foo(1, 2, 3) and 4) -- the arguments to foo stop to the left of the and. This seems silly in a contrived example, so let's turn it into a common perl idiom:

open $fh, '<', $filename or die "$! opening $filename";

which is equivalent to

(open($fh, '<', $filename) or die("$! opening $filename"));

which is actually exactly equivalent to (and compiles to)

die "$! opening $filename" unless open $fh, '<', $filename;

using the statement-modifier form of unless (which isn't an operator at all, is allowed only once per statement, and only comes at the end of a statement, so it doesn't really engage in precedence at all, but you could consider it as "lower than lowest" precedence leftwards).

So anyway, returning to your original code sample -- the arguments to print end just left of the and, and to the right of the and is a completely separate comma expression — which does nothing at all because it's just a few constants 3 > 4 and "\n" evaluated in void context.

明媚殇 2024-09-20 14:47:29

以下是这些语句在使用完整括号时的外观:

print("Test one: ", ((6 > 3) && (3 > 4)), "\n");
print("Test two: ", (6 > 3)) and ((3 > 4), "\n");

> 具有最高优先级,然后是 &&,然后是 ,,然后是 打印,然后

<代码>6> 3 的计算结果为 1。 <代码>3> 4 计算结果为 false,这在 Perl 中是一个特殊值,在数字上下文中为 0,但在字符串上下文中为空字符串(如下所示)。因此 ((6 > 3) && (3 > 4)) 产生空字符串。

因此,第一个语句将 3 个参数传递给 print"Test one: "、空字符串和换行符。它按顺序打印每个参数。

第二条语句仅向 print 传递 2 个参数:“测试二:”1。换行符不会被打印,因为它从未被传递给 print

我不知道如何比 文档。但也许让您感到困惑的是您认为存在“向左列表操作”和“向右列表操作”。不是这个意思。它试图说列表运算符右侧的所有内容都被解释为该运算符的参数(除非您使用 等优先级非常低的布尔运算符之一)。但列表运算符左侧的内容与该列表运算符无关。

Here's how those statements would look with full parentheses:

print("Test one: ", ((6 > 3) && (3 > 4)), "\n");
print("Test two: ", (6 > 3)) and ((3 > 4), "\n");

> has the highest precedence, then &&, then ,, then print, then and.

6 > 3 evaluates to 1. 3 > 4 evaluates to false, which in Perl is a special value that is 0 in a numeric context but the empty string in a string context (like here). So ((6 > 3) && (3 > 4)) yields the empty string.

Thus, the first statement passes 3 arguments to print: "Test one: ", the empty string, and a newline. It prints each argument in order.

The second statement passes only 2 arguments to print: "Test two: " and 1. The newline doesn't get printed because it never got passed to print.

I'm not sure how to explain "leftward" and "rightward" better than the docs. But maybe what's confusing you is that you're thinking there are "leftward list ops" and "rightward list ops". That's not what it means. It's trying to say that everything on the right-hand side of a list operator gets interpreted as the arguments for that operator (unless you hit one of the very-low-precedence Boolean operators like and). But things on the left-hand side of a list operator are not associated with that list operator.

冷情妓 2024-09-20 14:47:29

正如你所说,除了“这没有意义”部分。

第一个是

print "Test one: ", (6 > 3 && 3 > 4), "\n";

,第二个

(print "Test two: ", 6 > 3) and (3 > 4, "\n");

如果您打开警告(使用警告),您会收到警告在 void 上下文中无用使用常量,因为 的右侧and 计算结果为包含元素 false 和换行符的列表,但从未使用过。

编辑:更正了我的3>4 is undef声明。 False 不是 undef。它已定义并且不打印任何内容。

It is as you say, except for the "this doesn't make sense" part.

The first is

print "Test one: ", (6 > 3 && 3 > 4), "\n";

and the second

(print "Test two: ", 6 > 3) and (3 > 4, "\n");

If you turn on warnings (use warnings) you get the warning Useless use of a constant in void context because the right side of the and evaluates to a list with elements false and a newline but isn't ever used.

edit: Corrected my 3>4 is undef claim. False is not undef. It's defined and prints nothing.

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