简体   繁体   English

为什么`my $x = if (0) {1} else {2}` 不起作用?

[英]Why doesn't `my $x = if (0) {1} else {2}` work?

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

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

This makes sense, because if conditionals are not expressions in Perl.这是有道理的,因为if条件不是 Perl 中的表达式。 They're flow control.他们是流量控制。

But then但是之后

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

Does work!行得通! How come a do BLOCK can accept an if conditional?为什么do BLOCK可以接受if条件? But assignment can not?但是赋值不行吗? It would seem in the above the flow control must either在上面看来,流量控制必须要么

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

Moreover given the simple facts above, what is the right way to describe an if-conditional that behaves like that?此外,鉴于上面的简单事实,描述具有这种行为的 if 条件的正确方法是什么? Is it an expression with a value?它是一个带有值的表达式吗? Is it flow-control construct that has no value after evaluation?流控制结构是否在评估后没有价值?

And, lastly, what modifications would have to be made to assignment to have it accept an if condition like a do BLOCK .最后,必须对赋值进行哪些修改才能使其接受像do BLOCK这样的if条件。

The only answer and commentary that addresses the question in a matter that brings clarity is Ginnz,解决问题的唯一答案和评论是 Ginnz,

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 = .总体设计是 perl 具有仅在某些上下文中有意义的关键字,并且if是其中之一 - 它只能是语句的开头或语句修饰符,并且它们都不能直接在=之后有效。 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.除此之外,“ if语句返回什么”的考虑并不总是直观的,因此虽然您可以通过将其放在 do 块或子例程的最后一条语句中来发现这一点,但不应该鼓励使用它作为一个值。 In fact it commonly leads to bugs .事实上,它通常会导致错误 – Grinnz 11 hours ago – Grinnz 11 小时前

When Ginnz says parser distinction, I interpret that to mean that this isn't much worth pursing as a matter of clarity.当 Ginnz 说解析器的区别时,我将其解释为意味着为了清楚起见,这并不值得追求。 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语句,并且没有真正的理由,只是它是这样的。

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

Code inside DO block behaves differently. DO 块内的代码表现不同。 Perl interprets the code inside the curly brackets and outputs the result of the last command. Perl 解释大括号内的代码并输出最后一个命令的结果。


    #!/usr/bin/perl
    
    use Data::Dumper;
    
    my $x = do { { 1==1 } };
    my $y = do { { 1==0 } };
    print Dumper 1==0;
    print Dumper $x;
    print Dumper $y;

Read more about do: https://perldoc.perl.org/perlfunc#do阅读有关 do 的更多信息: https : //perldoc.perl.org/perlfunc#do

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM