小编Car*_*eVS的帖子

从子例程结束循环的迭代

我有一个带有while循环的程序,它有几个点,某些条件需要采取某些操作,然后跳过剩余的迭代.

因为这总是相同的代码,我想把它放在子程序中,但当我尝试使用'next;'时 作为子中的最后一个语句,我得到一个警告(通过下一个退出子程序......),虽然它似乎按预期工作.

即没有子:

while (#condition) {

    ## do stuff

    if (#condition to skip iteration) {
        ## action
        next;
    }

    ## do more stuff, with the above block repeated several times
}
Run Code Online (Sandbox Code Playgroud)

与子:

while (#condition) {

    ## do stuff

    &skip if (#condition to skip iteration);

    ## do more stuff, with more calls to &skip
}

sub skip() {
    ## action
    next;
}
Run Code Online (Sandbox Code Playgroud)

块/子中的其余代码是如此之短以至于除了下一个之外的所有代码; sub中的语句几乎违背了使用子例程的对象.

我的问题是:

  • 正在使用'next;' 以这种方式在子程序中好,还是危险/差的做法?
  • 有没有更好的方法从子例程跳过剩余的迭代?
  • 如果没有问题并且没有更好的方法,有没有办法抑制警告?

perl loops subroutine

7
推荐指数
3
解决办法
1218
查看次数

标签 统计

loops ×1

perl ×1

subroutine ×1