Perl 三元条件运算符内部赋值问题
我的程序中的这段 Perl 代码给出了错误的结果。
$condition ? $a = 2 : $a = 3 ;
print $a;
不管$condition
的值是多少,输出总是3,为什么呢?
This snippet of Perl code in my program is giving the wrong result.
$condition ? $a = 2 : $a = 3 ;
print $a;
No matter what the value of $condition
is, the output is always 3, how come?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Perl 文档对此进行了解释。
由于 Perl 运算符优先级,该语句被解析为
因为 ?: 运算符产生可分配的结果,所以 3 被分配给条件的结果。
当 $condition 为 true 时,这意味着 ($a=2)=3 给出 $a=3
当 $condition 为 false 时,这意味着 ($a)=3 给出 $a=3
正确的写法是
We got bitten by this在工作中,所以我在这里发帖希望其他人会发现它有用。
This is explained in the Perl documentation.
Because of Perl operator precedence the statement is being parsed as
Because the ?: operator produces an assignable result, 3 is assigned to the result of the condition.
When $condition is true this means ($a=2)=3 giving $a=3
When $condition is false this means ($a)=3 giving $a=3
The correct way to write this is
We got bitten by this at work, so I am posting here hoping others will find it useful.
一旦你意识到你可能遇到了优先级问题,就有一个技巧可以弄清楚 Perl 认为你的意思:
在你的情况下,这会告诉你:
......在这一点上你应该说“哦,这解释了它”!
Once you have an inkling that you might be suffering from precedence problems, a trick to figure out what Perl thought you meant:
In your case, that'll show you:
...at which point you should be saying "oh, that explains it"!
只是为了扩展前面的答案...如果出于某种原因,赋值需要成为条件的一部分,您需要这样写:
如果您根据条件分配给不同的变量,这将很有用。
如果您选择变量,但无论如何分配相同的内容,您甚至可以这样做:
Just to extend the previous answer... If, for whatever reason, the assignments need to be part of the conditional, you'd want to write it thusly:
This would be useful if you're assigning to different variables based on the condition.
And if you're choosing the variable, but assigning the same thing no matter what, you could even do this:
由于 Perl 运算符优先级,该语句被解析为:
因为 ?: 运算符产生可赋值的结果,所以 3 被赋值给条件的结果。
当 $condition 为 true 时,这意味着 $a=2=3 给出 $a=3
当 $condition 为 false 时,这意味着 $a=3 给出 $a=3
正确的写法是
一般来说,你应该真正摆脱使用条件语句进行赋值的习惯,就像在最初的例子中一样——正是这种习惯导致 Perl 获得了只写的声誉。
一个好的经验法则是,条件语句仅适用于简单值,而不适用于具有副作用的表达式。 当你或其他人需要八个月后阅读这段代码时,你会更喜欢它这样读吗?
或者像这样?
Because of Perl operator precedence the statement is being parsed as:
Because the ?: operator produces an assignable result, 3 is assigned to the result of the condition.
When $condition is true this means $a=2=3 giving $a=3
When $condition is false this means $a=3 giving $a=3
The correct way to write this is
In general, you should really get out of the habit of using conditionals to do assignment, as in the original example -- it's the sort of thing that leads to Perl getting a reputation for being write-only.
A good rule of thumb is that conditionals are only for simple values, never expressions with side effects. When you or someone else needs to read this code eight months from now, would you prefer it to read like this?
Or like this?
对上面 Tithium 答案的一个建议:
如果您想为同一个变量分配不同的值,这可能会更好(抄写方式):
$a = ($condition) ? 2:3;
One suggestion to Tithonium's answer above:
If you are want to assign different values to the same variable, this might be better (the copy-book way):
$a = ($condition) ? 2 : 3;