Search code examples
perlsmartmatch

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?


Solution

  • 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.