问题描述
根据 doc,while
只要表达式为 true,语句就会执行块.我想知道为什么它变成了一个带有空表达式的无限循环:
According to the doc, the while
statement executes the block as long as the expression is true. I wonder why it becomes an infinite loop with an empty expression:
while () { # infinite loop
...
}
这只是文档不准确吗?
推荐答案
$ perl -MO=Deparse -e 'while () { }'
while (1) {
();
}
-e syntax OK
看起来while () {}
和while (1) {}
是等价的.还要注意空括号*被插入到空块中.
It seems that while () {}
and while (1) {}
are equivalent. Also note that empty parens* are inserted in the empty block.
预定义编译器行为的另一个示例:
Another example of pre-defined compiler behaviour:
$ perl -MO=Deparse -e 'while (<>) { }'
while (defined($_ = <ARGV>)) {
();
}
-e syntax OK
我会说这只是没有报告特殊情况的文档.
I would say that this is just the docs not reporting a special case.
* —准确地说,插入了 stub
操作码.它什么都不做,但为 enterloop
操作码提供了一个 goto 目标.没有真正的理由要注意这一点.Deparse 使用空括号表示这个 stub
操作,因为括号不生成代码.
* — To be precise, the stub
opcode is inserted. It does nothing, but serves a goto target for the enterloop
opcode. There's no real reason to note this. Deparse denotes this stub
op using empty parens, since parens don't generate code.
这篇关于Perl:虽然没有条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!