为什么 Perl 控制语句需要大括号?

发布于 2024-08-15 02:42:48 字数 430 浏览 9 评论 0 原文

这可能看起来像最近提出的问题 为什么 Perl 不这样做允许单行“解锁”,,但我发现这个问题的答案并不令人满意,因为它们要么引用了语法文档说需要大括号,我认为这只是在回避问题,或者忽略了问题并简单地给出了无大括号的替代方案。

为什么 Perl 需要大括号来控制语句,例如 iffor?换句话说,为什么 Perl 需要块而不是语句,就像其他一些流行语言所允许的那样?

This may look like the recent question that asked why Perl doesn't allow one-liners to be "unblocked," but I found the answers to that question unsatisfactory because they either referred to the syntax documentation that says that braces are required, which I think is just begging the question, or ignored the question and simply gave braceless alternatives.

Why does Perl require braces for control statements like if and for? Put another way, why does Perl require blocks rather than statements, like some other popular languages allow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

情域 2024-08-22 02:42:48

一个原因可能是某些样式规定您应该始终使用带有控制结构的大括号,即使对于一个衬垫也是如此,以避免以后破坏它们,例如:

if (condition) 
   myObject.doSomething();
else 
   myObject.doSomethingElse();

然后有人在第一部分中添加了更多内容:

if (condition)
   myObject.doSomething();
   myObject.doSomethingMore(); // Syntax error next line
else 
   myObject.doSomethingElse();

或者更糟:

if (condition)
   myObject.doSomething();
else 
   myObject.doSomethingElse();
   myObject.doSomethingMore(); // Compiles, but not what you wanted.

在 Perl 中,这些各种错误是不可能的,因为不使用带有控制结构的大括号总是语法错误。实际上,样式决策已在语言语法级别强制执行。

这是否是真正原因的一部分,只有拉里的胡子知道。

One reason could be that some styles dictate that you should always use braces with control structures, even for one liners, in order to avoid breaking them later, e.g.:

if (condition) 
   myObject.doSomething();
else 
   myObject.doSomethingElse();

Then someone adds something more to the first part:

if (condition)
   myObject.doSomething();
   myObject.doSomethingMore(); // Syntax error next line
else 
   myObject.doSomethingElse();

Or worse:

if (condition)
   myObject.doSomething();
else 
   myObject.doSomethingElse();
   myObject.doSomethingMore(); // Compiles, but not what you wanted.

In Perl, these kinds of mistakes are not possible, because not using braces with control structures is always a syntax error. In effect, a style decision has been enforced at the language syntax level.

Whether that is any part of the real reason, only Larry's moustache knows.

绮烟 2024-08-22 02:42:48

原因之一可能是某些构造在没有大括号的情况下会不明确:

foreach (@l) do_something unless $condition;

unless $condition 是否适用于整个事物或仅适用于 do_something 语句?

当然,这可以通过优先级规则或其他东西来解决,
但这将是创建令人困惑的 Perl 代码的另一种方式:-)

One reason could be that some constructs would be ambiguous without braces :

foreach (@l) do_something unless $condition;

Does unless $condition apply to the whole thing or just the do_something statement?

Of course this could have been worked out with priority rules or something,
but it would have been yet another way to create confusing Perl code :-)

陌上芳菲 2024-08-22 02:42:48

无括号 if-else 子句的一个问题是它们可能导致语法歧义:

if (foo)
    if (bar)
       mumble;
    else
       tumble;

鉴于上述情况,在什么条件下执行 tumble?它可以被解释为发生在 !foofoo && 时。 !栏。添加大括号可以消除歧义,而不会过多地污染源代码。然后你可以继续说,使用大括号总是一个好主意,所以让我们让语言需要它并解决关于是否应该使用它们的无休止的 C 争论。或者,当然,您可以通过完全去掉大括号并使用缩进来指示嵌套来解决问题。两者都是使清晰、明确的代码变得自然而然的方法,而不需要特殊的努力。

One problem with braceless if-else clauses is they can lead to syntactic ambiguity:

if (foo)
    if (bar)
       mumble;
    else
       tumble;

Given the above, under what condition is tumble executed? It could be interpreted as happening when !foo or foo && !bar. Adding braces clears up the ambiguity without dirtying the source too much. You could then go on to say that it's always a good idea to have the braces, so let's make the language require it and solve the endless C bickering over whether they should be used or not. Or, of course, you could address the problem by getting rid of the braces completely and using the indentation to indicate nesting. Both are ways of making clear, unambiguous code a natural thing rather than requiring special effort.

面犯桃花 2024-08-22 02:42:48

在《Programming Perl》(Larry Wall 合着),第 3 版,第 113 页中,复合语句是根据表达式和块而不是语句来定义的,并且块有大括号。

请注意,与 C 和 Java 不同,
[复合语句] 定义于
BLOCKS 的术语,而不是语句。
这意味着大括号是
必需的——没有悬挂语句
允许。

我不知道这是否回答了你的问题,但在这种情况下,他似乎选择支持简单的语言结构而不是例外。

In Programming Perl (which Larry Wall co-authored), 3rd Edition, page 113, compound statements are defined in terms of expressions and blocks, not statements, and blocks have braces.

Note that unlike in C and Java,
[compound statements] are defined in
terms of BLOCKS, not statements.
This means that the braces are
requried--no dangling statements
allowed.

I don't know if that answers your question but it seems like in this case he chose to favor a simple language structure instead of making exceptions.

嗫嚅 2024-08-22 02:42:48

也许与您关于(大概)Perl 5 及更早版本的问题没有直接关系,但是......

在 Perl 6 中,控制结构不需要括号:

if $x { say '$x is true' }

for <foo bar baz> -> $s { say "[$s]" }

如果大括号也是可选的,那么这将是非常不明确的。

Perhaps not directly relevant to your question about (presumably) Perl 5 and earlier, but…

In Perl 6, control structures do not require parentheses:

if $x { say '$x is true' }

for <foo bar baz> -> $s { say "[$s]" }

This would be horrendously ambiguous if the braces were also optional.

往日情怀 2024-08-22 02:42:48

Perl不是允许你跳过大括号,但是你必须在条件之前写语句吗? IE

#!/usr/bin/perl

my $a = 1;

if ($a == 1) {
    print "one\n";
}

# is equivalent to:

print "one\n" if ($a == 1);

Isn't it that Perl allows you to skip the braces, but then you have to write statement before condition? i.e.

#!/usr/bin/perl

my $a = 1;

if ($a == 1) {
    print "one\n";
}

# is equivalent to:

print "one\n" if ($a == 1);
空城仅有旧梦在 2024-08-22 02:42:48

好吧,通常情况下,您需要在块周围使用大括号,但如果该块只有一个语句长,则不需要,当然,除非您的语句以某种方式不明确,并且受优先级规则的约束,而与您不同如果你省略了大括号——在这种情况下,你也可以想象使用括号,但这会不一致,因为它毕竟是一个块——这当然取决于所涉及运算符各自的优先级。无论如何,您不需要在右大括号后放置分号 - 如果您结束 if 语句,后跟 else 语句,甚至是错误的 - 除非您绝对必须在标头末尾放置分号文件是用 C++ 编写的(或者是 C?)。

说真的,我很高兴代码中的每一个明确性和统一性。

"Okay, so normally, you need braces around blocks, but not if the block is only one statement long, except, of course, if your statement would be ambiguous in a way that would be ruled by precedence rules not like you want if you omitted the braces -- in this case, you could also imagine the use of parentheses, but that would be inconsistent, because it is a block after all -- this is of course dependent on the respective precedence of the involved operators. In any case, you don't need to put semicolons after closing braces -- it is even wrong if you end an if statement that is followed by an else statement -- except that you absolutely must put a semicolon at the end of a header file in C++ (or was it C?)."

Seriously, I am glad for every explicitness and uniformity in code.

浮光之海 2024-08-22 02:42:48

只是在这里猜测,但是“畅通无阻”的循环/ifs/等等。往往是在代码维护期间引入微妙错误的地方,因为草率的维护者可能会尝试在“循环内部”添加另一行,而没有意识到它实际上并不在内部。

当然,这是我们正在谈论的 Perl,所以可能任何依赖于可维护性的论点都是可疑的......:)

Just guessing here, but "unblocked" loops/ifs/etc. tend to be places where subtle bugs are introduced during code maintenance, since a sloppy maintainer might try to add another line "inside the loop" without realizing that it's not really inside.

Of course, this is Perl we're talking about, so probably any argument that relies on maintainability is suspect... :)

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