【问题标题】:Are braces required in if condition in perl?perl中的if条件是否需要大括号?
【发布时间】:2020-08-04 14:18:21
【问题描述】:

这段代码:

#!/usr/bin/perl -w

sub marine{
    print "somestuff\n";
    exit 1;
}
$bool=1;

if($bool)
    marine();

给出这个错误:

Bareword found where operator expected at ./a line 10, near ")
    marine"
    (Missing operator before marine?)
syntax error at ./a line 10, near ")
    marine"
Execution of ./a aborted due to compilation errors.

但是,当 if 正文中有 {} 时,它可以工作。 Perl 是否需要它们?

【问题讨论】:

    标签: perl if-statement curly-braces


    【解决方案1】:

    对于Compound statements,和你一样,大括号是必要的。

    if ($bool) {
        marine();
    }
    

    但是,Statement modifiers 不需要大括号:

    marine() if $bool;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-01
      • 2010-09-27
      • 2011-11-11
      • 2020-09-07
      • 2022-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多