当后面跟着一个 BLOCK 时,continue 实际上是一个流程控制语句,而不是一个函数。如果有一个 continue BLOCK 附加到一个 BLOCK(通常在 while 或 foreach 中),它总是在条件即将再次评估之前执行,就像 C 中 for 循环的第三部分一样。因此,它可以用来递增循环变量,即使循环是通过 next 语句(类似于 C 的 continue 语句)继续的。
last、next 或 redo 可能会出现在 continue 块中;last 和 redo 的行为就像它们是在主块中执行的一样。 next 也是如此,但由于它会执行 continue 块,所以它可能更有趣。
while (EXPR) {
### redo always comes here
do_something;
} continue {
### next always comes here
do_something_else;
# then back the top to re-check EXPR
}
### last always comes here
省略 continue 部分等同于使用一个空的 continue 部分,逻辑上来说,所以 next 会直接回到循环顶部检查条件。
当没有 BLOCK 时,continue 是一个函数,它会从当前的 when 或 default 块中跳过,而不是迭代动态封闭的 foreach 或退出词法封闭的 given。在 Perl 5.14 及更早版本中,这种形式的 continue 只有在启用了 "switch" 特性 时才可用。有关更多信息,请参见 feature 和 "Switch Statements" in perlsyn。