温馨提示:本文翻译自stackoverflow.com,查看原文请点击:perlsyn - Meaning of the perl syntax construction involving a comma
perl perlsyn

perlsyn - 包含逗号的perl语法构造的含义

发布于 2020-04-10 17:17:18

我在一本书中遇到了一段代码,如下所示:

#for (some_condition) {
#do something not particularly related to the question
$var = $anotherVar+1, next if #some other condition with $var
#}

我不知道$ anotherVar + 1下一个之间的逗号(“,”)是什么。该语法构造如何调用,甚至正确?

查看更多

提问者
Ivan
被浏览
108
Rob Sweet 2020-02-02 01:30

考虑以下代码:

$x=1;
$y=1;

$x++ , $y++ if 0; # note the comma! both x and y are one statement
print "With comma: $x $y\n";

$x=1;
$y=1;

$x++ ; $y++ if 0; # note the semicolon! so two separate statements

print "With semicolon: $x $y\n";

输出如下:

With comma: 1 1
With semicolon: 2 1

逗号类似于分号,区别在于命令的两侧都被视为一条语句。这意味着在只需要一个语句的情况下,逗号的两端都会被评估。