Break 是否在给定/何时(又名开关)之外定义了行为?
更新
正如 @M42 所指出的,明显的问题只是由于我的示例脚本中的拼写错误造成的。 eval
隐藏了由于在没有 given
的情况下进行 break
导致的错误。 $@
中的错误信息是:
Can't "break" outside a given block
所以情况很清楚,完全没有问题。要结束这个问题了。
不过,另一个更新
似乎有点不一致;在一种情况下会发出警告,但在另一种情况下不会发出警告:
$ perl5.12.4 -WE "eval { say 0; say 1 if last; say 2 }; warn if $@; say 3"
0
Can't "break" outside a given block at -e line 1.
...caught at -e line 1.
3
$ perl5.12.4 -WE "eval { say 0; say 1 if last; say 2 }; warn if $@; say 3"
0
Exiting eval via last at -e line 1.
Can't "last" outside a loop block at -e line 1.
...caught at -e line 1.
3
perl5.14.1
的输出相同。
想知道为什么这些不是编译时错误。毕竟它们是语法错误。
该消息表示,当实现证明我们可以通过 last
或 break
退出时,我们无法退出 - 程序只是继续运行。 eval
真的应该捕获这样的损坏代码吗?只是一个用户想知道......
原始帖子
在代码审查中,我刚刚看到复制/粘贴的 given/when/break
其中 given/when
被删除,并且 < code>break 被剩下了。该代码编译并运行时没有警告。这让我想到了一个问题:
break
是否在 given/when
之外定义了行为?
我在 perlsyn
中找不到任何权威内容。我无法从以下程序的输出中看出什么是 break
原理:
use 5.010;
use strict;
use warnings;
sub bla {
my @args = @_;
eval {
for ( @args ) {
say;
if ( $_ eq 'blub' ) {
say '==> break';
break;
}
say 'nach if';
}
say 'nach for';
};
say 'eval Ende';
say 'sub Ende';
}
bla qw/ eins zwei blub drei /;
say for '-' x 40;
eval {
say 'im eval';
say '==> break';
break:
say 'immer noch im eval';
};
say 'nach dem eval';
与 5.10.1、5.12.4 和 5.14.1 的输出相同。请注意,在第一个示例中,所有 eval/for/if
都被跳过,而在第二个示例中,eval
继续。
eins
nach if
zwei
nach if
blub
==> break
eval Ende
sub Ende
----------------------------------------
im eval
==> break
immer noch im eval
nach dem eval
那么这是未定义的行为吗?或者您有相关的文档指针吗?
Update
The apparent problem was only due to a typo in my sample script, as noted by @M42. The eval
hid the error caused by doing a break
without given
. The error message in $@
is:
Can't "break" outside a given block
So the case is clear and there is no problem at all. Going to close the question.
Another Update
Seems a little inconsistent, though; warning in one case but not in the other:
$ perl5.12.4 -WE "eval { say 0; say 1 if last; say 2 }; warn if $@; say 3"
0
Can't "break" outside a given block at -e line 1.
...caught at -e line 1.
3
$ perl5.12.4 -WE "eval { say 0; say 1 if last; say 2 }; warn if $@; say 3"
0
Exiting eval via last at -e line 1.
Can't "last" outside a loop block at -e line 1.
...caught at -e line 1.
3
Same output for perl5.14.1
.
Wondering why those aren't compile-time errors anyway. They are syntax errors after all.
And the message says we can't exit via last
or break
when the implementation proves we can - the program simply carries on. Should eval
really catch such broken code? Just a user wondering ...
Original post
In a code review, I just saw a copied/pasted given/when/break
where the given/when
was deleted and the break
was left over. The code compiles and runs without warnings. Which led me to the question:
Does break
have defined behaviour outside of given/when
?
I couldn't find anything authoritative in perlsyn
. And I can't see what would be the break
-rationale from the output of the following program:
use 5.010;
use strict;
use warnings;
sub bla {
my @args = @_;
eval {
for ( @args ) {
say;
if ( $_ eq 'blub' ) {
say '==> break';
break;
}
say 'nach if';
}
say 'nach for';
};
say 'eval Ende';
say 'sub Ende';
}
bla qw/ eins zwei blub drei /;
say for '-' x 40;
eval {
say 'im eval';
say '==> break';
break:
say 'immer noch im eval';
};
say 'nach dem eval';
Identical output with 5.10.1, 5.12.4 and 5.14.1. Note in the first example all of eval/for/if
are skipped, whereas in the second example eval
continues.
eins
nach if
zwei
nach if
blub
==> break
eval Ende
sub Ende
----------------------------------------
im eval
==> break
immer noch im eval
nach dem eval
So is it undefined behaviour? Or do you have any relevant doc pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在break指令后面放置分号而不是冒号。
带有冒号的情况下,它被视为标签。
Put a semicolon instead of a colon after the break instruction
With a colon it is considered as a label.