所有不同的 Perl 6 相等运算符是怎么回事? (==、===、eq、eqv、~~、=:=、...)
Perl 6 似乎有大量的相等运算符。 什么是=:=
? leg
和 cmp
之间有什么区别? 或者 eqv
和 ===
?
有谁有好的总结吗?
Perl 6 seems to have an explosion of equality operators. What is =:=
? What's the difference between leg
and cmp
? Or eqv
and ===
?
Does anyone have a good summary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
=:= 测试是否有两个容器(变量或数组或哈希项)是别名的,即如果其中一个发生变化,另一个也会发生变化吗?
至于其他: === 测试两个引用是否指向同一个对象,并且 eqv 测试两个事物在结构上是否等效。 所以
[1, 2, 3] === [1, 2, 3]
将为 false(不是同一个数组),但是[1, 2, 3] eqv [1, 2, 3]
将为 true(相同的结构)。leg
比较字符串,如 Perl 5 的cmp
,而 Perl 6 的cmp
更智能,会比较数字,如<=>
code> 和诸如leg
之类的字符串。最后
~~
是“智能匹配”,它回答“$x
是否匹配$y
”的问题。 如果$y
是类型,则进行类型检查。 如果$y
是正则表达式,则它是正则表达式匹配 - 依此类推。=:= tests if two containers (variables or items of arrays or hashes) are aliased, ie if one changes, does the other change as well?
As for the others: === tests if two references point to the same object, and eqv tests if two things are structurally equivalent. So
[1, 2, 3] === [1, 2, 3]
will be false (not the same array), but[1, 2, 3] eqv [1, 2, 3]
will be true (same structure).leg
compares strings like Perl 5'scmp
, while Perl 6'scmp
is smarter and will compare numbers like<=>
and strings likeleg
.Finally
~~
is the "smart match", it answers the question "does$x
match$y
". If$y
is a type, it's type check. If$y
is a regex, it's regex match - and so on.概要 3:比较语义中的摘要是否符合您的要求,或者您已经阅读过那? 设计文档链接到使用这些功能的测试文件,因此您可以查看它们的使用示例及其当前的测试状态。
Perl 6 的比较运算符更适合动态语言和所有正在发生的事情。 现在,您可以使用执行您想要的操作的运算符精确地测试事物,而不仅仅是比较字符串或数字(或将事物转换为字符串或数字)。 您可以测试值、容器、类型等。
在其中一条评论中,您询问了
eqv
和cmp
。 在 Perl 5 的旧时代,cmp
用于排序并返回三个神奇值 (-1,0,1) 之一,并且它始终使用字符串语义来实现这一点。 在 Perl 6 中,cmp
返回三种类型的Order
对象之一,因此您不必记住 -1、0 或 1 的含义。 此外,新的cmp
不会强制使用字符串语义,因此在处理数字时它会更加智能(与 Perl 5 不同,Perl 5 的排序方式为 1, 10, 11, 2, 20, 21 ...) 。leg
(lessthan、equal、greeaterthan)是cmp
具有字符串语义。 它被定义为 Perl 6 的~$a cmp ~$b
,其中~
是强制字符串语义的新“字符串上下文生成器”。 使用leg
,您总是进行字符串比较,就像旧的 Perl 5cmp
一样。如果您对其他操作员仍有疑问,让我们将它们分解为单独的问题。 :)
Does the summary in Synopsis 3: Comparison semantics do what you want, or were you already reading that? The design docs link to the test files where those features are used, so you can see examples of their use and their current test state.
Perl 6's comparison operators are much more suited to a dynamic language and all of the things going on. Instead of just comparing strings or numbers (or turning things into strings or numbers), now you can test things precisely with an operator that does what you want. You can test the value, the container, the type, and so on.
In one of the comments, you ask about
eqv
andcmp
. In the old days of Perl 5,cmp
was there for sorting and returns one of three magic values (-1,0,1), and it did that with string semantics always. In Perl 6,cmp
returns one of three types ofOrder
objects, so you don't have to remember what -1, 0, or 1 means. Also, the newcmp
doesn't force string semantics, so it can be smarter when handed numbers (unlike Perl 5's which would sort like 1, 10, 11, 2, 20, 21 ...).The
leg
(less than, equal, greater than) iscmp
with string semantics. It's defined as Perl 6's~$a cmp ~$b
, where~
is the new "string contextualizer" that forces string semantics. Withleg
, you are always doing a string comparison, just like the old Perl 5cmp
.If you still have questions on the other operators, let's break them down into separate questions. :)