为什么`my $ x = if(0){1} else {2}`不起作用? [英] Why doesn't `my $x = if (0) {1} else {2}` work?

查看:95
本文介绍了为什么`my $ x = if(0){1} else {2}`不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在perl中,$x = if (0) {1} else {2}不起作用.

In perl, $x = if (0) {1} else {2} does not work.

$ perl -E'$x = if (0) {1} else {2}'
syntax error at -e line 1, near "= if"
Execution of -e aborted due to compilation errors.

这很有意义,因为if条件不是Perl中的表达式.它们是流量控制.

This makes sense, because if conditionals are not expressions in Perl. They're flow control.

但是然后

my $x = do { if (0) {1} else {2} };

行得通! do BLOCK为何可以接受if有条件?但是分配不能吗?在上面看来,流量控制必须要么

Does work! How come a do BLOCK can accept an if conditional? But assignment can not? It would seem in the above the flow control must either

  • 知道它在do BLOCK
  • 中的上下文
  • 总是充当表达式,但是解析器不允许该语法.
  • know it's context in a do BLOCK
  • always act as an expression, but have that syntax disallowed by the parser.

此外,鉴于上述简单事实,描述行为类似的if条件的正确方法是什么?它是带有值的表达式吗?评估后没有价值的流程控制构造吗?

Moreover given the simple facts above, what is the right way to describe an if-conditional that behaves like that? Is it an expression with a value? Is it flow-control construct that has no value after evaluation?

最后,必须对赋值进行什么修改才能使其接受像do BLOCK这样的if条件.

And, lastly, what modifications would have to be made to assignment to have it accept an if condition like a do BLOCK.

推荐答案

唯一能解决问题的答案和评论是吉宁兹(Ginnz),

The only answer and commentary that addresses the question in a matter that brings clarity is Ginnz,

总体设计是perl具有仅在某些上下文中有意义的关键字,而if是其中之一-它只能是语句的开头或语句修饰符,而这些都不是有效的直接在=之后. 这首先是解析器的区别.此外,对"if语句返回的内容"的考虑并不总是很直观,因此尽管您可以通过将其放在do块中找出来或子程序的最后一条语句,不应鼓励将其用作值.实际上,它通常会导致错误. – Grinnz 11小时前

The over-arching design is that perl has keywords that only are meaningful in certain contexts, and if is one of them - it can be only the start of a statement or a statement modifier, and neither of those are valid directly after an =. This is a parser distinction first and foremost. Beyond that, the consideration of "what an if statement returns" is not always intuitive, so while you can find that out by putting it in a do block or the last statement of a subroutine, it's not something that should be encouraged to be used as a value. In fact it commonly leads to bugs. – Grinnz 11 hours ago

当Ginnz说到解析器区别时,我的解释是,出于清晰的考虑,这并不值得追求.在=之后有效的内容根本不是if语句,并且没有真正的原因,除了它是这样.

When Ginnz says parser distinction, I interpret that to mean that this isn't much worth pursing as a matter of clarity. What is valid after an = is simply not a if statement, and there is no real reason for it, except that it's how it is.

  • if条件语句.
  • 语句的返回值.
  • 赋值=明确禁止右边的语句,因为它仅接受表达式类中的内容.
  • a do BLOCK可以将语句转换为表达式.
  • if conditional is a statement.
  • statement's return values.
  • assignment = expressly forbids statements on the right because it only accepts things in the expression class.
  • a do BLOCK can turn a statement into an expression.

这篇关于为什么`my $ x = if(0){1} else {2}`不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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