Hawramani Hawramani - 8 months ago 34
Perl Question

Why does smartmatch return different values depending on the order of the operands?

I have an array for which the following test returns true:

1 ~~ @a

And yet, the following test returns false:

@a ~~ 1

I read in Learning Perl that the placement of the values on either side of the smartmatch operator doesn't matter, but obviously in the above code it does. Why is that? Are the two statements checking different things?


In addition to the other answers, the list of Perl 5.10.1 changes has a section on changes made to the ~~ operator:

The smart match operator ~~ is no longer commutative. The behaviour of a smart match now depends primarily on the type of its right hand argument.

So Learning Perl may have been correct prior to these changes.