所有不同的 Perl 6 相等运算符是怎么回事? (==、===、eq、eqv、~~、=:=、...)

发布于 2024-07-06 23:52:38 字数 158 浏览 11 评论 0原文

Perl 6 似乎有大量的相等运算符。 什么是=:=legcmp 之间有什么区别? 或者 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 技术交流群。

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

发布评论

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

评论(2

最笨的告白 2024-07-13 23:52:38

=:= 测试是否有两个容器(变量或数组或哈希项)是别名的,即如果其中一个发生变化,另一个也会发生变化吗?

my $x;
my @a = 1, 2, 3;
# $x =:= @a[0] is false
$x := @a[0];
# now $x == 1, and $x =:= @a[0] is true
$x = 4;
# now @a is 4, 2, 3 

至于其他: === 测试两个引用是否指向同一个对象,并且 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 之类的字符串。

13 leg 4   # -1, because 1 is smaller than 4, and leg converts to string
13 cmp 4   # +1, because both are numbers, so use numeric comparison.

最后~~是“智能匹配”,它回答“$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?

my $x;
my @a = 1, 2, 3;
# $x =:= @a[0] is false
$x := @a[0];
# now $x == 1, and $x =:= @a[0] is true
$x = 4;
# now @a is 4, 2, 3 

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's cmp, while Perl 6's cmp is smarter and will compare numbers like <=> and strings like leg.

13 leg 4   # -1, because 1 is smaller than 4, and leg converts to string
13 cmp 4   # +1, because both are numbers, so use numeric comparison.

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.

世态炎凉 2024-07-13 23:52:38

概要 3:比较语义中的摘要是否符合您的要求,或者您已经阅读过那? 设计文档链接到使用这些功能的测试文件,因此您可以查看它们的使用示例及其当前的测试状态。

Perl 6 的比较运算符更适合动态语言和所有正在发生的事情。 现在,您可以使用执行您想要的操作的运算符精确地测试事物,而不仅仅是比较字符串或数字(或将事物转换为字符串或数字)。 您可以测试值、容器、类型等。

在其中一条评论中,您询问了 eqvcmp。 在 Perl 5 的旧时代,cmp 用于排序并返回三个神奇值 (-1,0,1) 之一,并且它始终使用字符串语义来实现这一点。 在 Perl 6 中,cmp 返回三种类型的 Order 对象之一,因此您不必记住 -1、0 或 1 的含义。 此外,新的 cmp 不会强制使用字符串语义,因此在处理数字时它会更加智能(与 Perl 5 不同,Perl 5 的排序方式为 1, 10, 11, 2, 20, 21 ...) 。

leglessthan、equal、greeaterthan)是cmp 具有字符串语义。 它被定义为 Perl 6 的 ~$a cmp ~$b,其中 ~ 是强制字符串语义的新“字符串上下文生成器”。 使用 leg,您总是进行字符串比较,就像旧的 Perl 5 cmp 一样。

如果您对其他操作员仍有疑问,让我们将它们分解为单独的问题。 :)

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 and cmp. 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 of Order objects, so you don't have to remember what -1, 0, or 1 means. Also, the new cmp 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) is cmp with string semantics. It's defined as Perl 6's ~$a cmp ~$b, where ~ is the new "string contextualizer" that forces string semantics. With leg, you are always doing a string comparison, just like the old Perl 5 cmp.

If you still have questions on the other operators, let's break them down into separate questions. :)

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