What's the deal with all the different Perl 6 equality operators? (==, ===, eq, eqv, ~~, =:=, …)

前端 未结 4 1026
臣服心动
臣服心动 2021-01-31 08:15

Perl 6 seems to have an explosion of equality operators. What is =:=? What\'s the difference between leg and cmp? Or eqv and

相关标签:
4条回答
  • 2021-01-31 08:30

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

    0 讨论(0)
  • 2021-01-31 08:35

    This is also a handy reference guide:

    Perl6 Periodic Table of Operators

    0 讨论(0)
  • 2021-01-31 08:41

    From reddit today:

    A comparison of the Perl equality operators

    0 讨论(0)
  • 2021-01-31 08:50

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

    0 讨论(0)
提交回复
热议问题