Perl 三元条件运算符内部赋值问题

发布于 2024-07-04 04:09:16 字数 152 浏览 7 评论 0原文

我的程序中的这段 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冷夜 2024-07-11 04:09:16

Perl 文档对此进行了解释。

由于 Perl 运算符优先级,该语句被解析为

($condition ? $a= 2 : $a ) = 3 ;

因为 ?: 运算符产生可分配的结果,所以 3 被分配给条件的结果。

当 $condition 为 true 时,这意味着 ($a=2)=3 给出 $a=3

当 $condition 为 false 时,这意味着 ($a)=3 给出 $a=3

正确的写法是

$a = ( $condition ? 2 : 3 );
print $a;

We got bitten by this在工作中,所以我在这里发帖希望其他人会发现它有用。

This is explained in the Perl documentation.

Because of Perl operator precedence the statement is being parsed as

($condition ? $a= 2 : $a ) = 3 ;

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

$a = ( $condition ? 2 : 3 );
print $a;

We got bitten by this at work, so I am posting here hoping others will find it useful.

白云不回头 2024-07-11 04:09:16

一旦你意识到你可能遇到了优先级问题,就有一个技巧可以弄清楚 Perl 认为你的意思:

perl -MO=Deparse,-p -e '$condition ? $a= 2 : $a= 3 ; print $a;'

在你的情况下,这会告诉你:

(($condition ? ($a = 2) : $a) = 3);
print($a);
-e syntax OK

......在这一点上你应该说“哦,这解释了它”!

Once you have an inkling that you might be suffering from precedence problems, a trick to figure out what Perl thought you meant:

perl -MO=Deparse,-p -e '$condition ? $a= 2 : $a= 3 ; print $a;'

In your case, that'll show you:

(($condition ? ($a = 2) : $a) = 3);
print($a);
-e syntax OK

...at which point you should be saying "oh, that explains it"!

只是为了扩展前面的答案...如果出于某种原因,赋值需要成为条件的一部分,您需要这样写:

$condition ? ($a=2) : ($a=3);

如果您根据条件分配给不同的变量,这将很有用。

$condition ? ($a=2) : ($b=3);

如果您选择变量,但无论如何分配相同的内容,您甚至可以这样做:

($condition ? $a : $b) = 3;

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:

$condition ? ($a=2) : ($a=3);

This would be useful if you're assigning to different variables based on the condition.

$condition ? ($a=2) : ($b=3);

And if you're choosing the variable, but assigning the same thing no matter what, you could even do this:

($condition ? $a : $b) = 3;
苏大泽ㄣ 2024-07-11 04:09:16

由于 Perl 运算符优先级,该语句被解析为:

($condition ? $a = 2 : $a ) = 3 ;

因为 ?: 运算符产生可赋值的结果,所以 3 被赋值给条件的结果。

当 $condition 为 true 时,这意味着 $a=2=3 给出 $a=3

当 $condition 为 false 时,这意味着 $a=3 给出 $a=3

正确的写法是

$a = $condition ? 2 : 3;

一般来说,你应该真正摆脱使用条件语句进行赋值的习惯,就像在最初的例子中一样——正是这种习惯导致 Perl 获得了只写的声誉。

一个好的经验法则是,条件语句仅适用于简单值,而不适用于具有副作用的表达式。 当你或其他人需要八个月后阅读这段代码时,你会更喜欢它这样读吗?

$x < 3 ? foo($x) : bar($y);

或者像这样?

if ($x < 3) {
  $foo($x);
} else {
  $bar($y);
}

Because of Perl operator precedence the statement is being parsed as:

($condition ? $a = 2 : $a ) = 3 ;

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

$a = $condition ? 2 : 3;

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?

$x < 3 ? foo($x) : bar($y);

Or like this?

if ($x < 3) {
  $foo($x);
} else {
  $bar($y);
}
GRAY°灰色天空 2024-07-11 04:09:16

对上面 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;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文