为什么 smartmatch 根据操作数的顺序返回不同的值? [英] Why does smartmatch return different values depending on the order of the operands?

查看:48
本文介绍了为什么 smartmatch 根据操作数的顺序返回不同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,其以下测试返回 true:

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

1 ~~ @a

然而,以下测试返回 false:

And yet, the following test returns false:

@a ~~ 1

我在 Learning Perl 中读到,值在 smartmatch 运算符两侧的位置并不重要,但显然在上面的代码中是重要的.这是为什么?这两个语句检查不同的东西吗?

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?

推荐答案

除了其他答案,Perl 5.10.1 更改列表 有一节介绍了对 ~~ 操作符所做的更改:

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.

所以学习 Perl 在这些变化之前可能是正确的.

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

这篇关于为什么 smartmatch 根据操作数的顺序返回不同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆