问题描述
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 ===
?
有人总结不错吗?
推荐答案
=:= 测试两个容器(变量或数组项或哈希项)是否被别名,即,如果其中一个发生变化,另一个容器是否也发生变化?
=:= 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
对于其他: === 测试是否有两个引用指向到同一个对象,然后 eqv 测试两个对象在结构上是否等效.因此[1, 2, 3] === [1, 2, 3]
将为false(不是相同的数组),但[1, 2, 3] eqv [1, 2, 3]
将为true(相同的结构).
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
比较Perl 5的cmp
之类的字符串,而Perl 6的cmp
则更聪明,并且将比较<=>
之类的数字和leg
之类的字符串.
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.
最后,~~
是智能匹配",它回答了问题是否$x
匹配$y
".如果$y
是类型,则为类型检查.如果$y
是正则表达式,则为正则表达式匹配-依此类推.
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.
这篇关于所有不同的Perl 6相等运算符有什么关系? (==,===,eq,eqv,~~,=:=,...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!