Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

raku - What's the deal with all the different Perl 6 equality operators? (==, ===, eq, 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?

question from:https://stackoverflow.com/questions/176343/whats-the-deal-with-all-the-different-perl-6-equality-operators-eq

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

=:= 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...