我在一本书中遇到了一段代码,如下所示:
#for (some_condition) {
#do something not particularly related to the question
$var = $anotherVar+1, next if #some other condition with $var
#}
我不知道$ anotherVar + 1和下一个之间的逗号(“,”)是什么。该语法构造如何调用,甚至正确?
考虑以下代码:
$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
逗号类似于分号,区别在于命令的两侧都被视为一条语句。这意味着在只需要一个语句的情况下,逗号的两端都会被评估。
这是重要的说明,谢谢