📘 Universal comparison operator eqv in Perl 6

eqv is an operator that tests the two operands for equivalence. It returns the True value if the operands are of the same type and contain the same values.

my $x = 3;
my $y = 3;
say $x eqv $y; # True

An example with a bit more complex data structures:

my @a = (3, 4);
my @b = (3, 4, 5);
@b.pop;
say @a eqv @b; # True

Note that because the integer and the floating-point types are different data types, comparing two equal numbers may give a False result. The same applies to the comparison with a string containing a numeric value.

say 42 eqv 42.0; # False
say 42 eqv "42"; # False

It is even trickier when one of the operands is of the Rat value.

say 42 eqv 84/2;       # False, 84/2 is Rat
say 42 eqv (84/2).Int; # True, the value is cast to Int

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s