mil*_*bos 1 perl if-statement curly-braces
这段代码:
#!/usr/bin/perl -w
sub marine{
print "somestuff\n";
exit 1;
}
$bool=1;
if($bool)
marine();
Run Code Online (Sandbox Code Playgroud)
给出这个错误:
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.
Run Code Online (Sandbox Code Playgroud)
但是,它{}在if体内时起作用。它们在 Perl 中是必需的吗?
对于像您这样的复合语句,大括号是必要的。
if ($bool) {
marine();
}
Run Code Online (Sandbox Code Playgroud)
但是,语句修饰符不需要大括号:
marine() if $bool;
Run Code Online (Sandbox Code Playgroud)