Perl Breaking out of an If statement

后端 未结 7 842
深忆病人
深忆病人 2021-02-05 03:50

This one just came up: How do I break out of an if statement? I have a long if statement, but there is one situation where I can break out of it early on.

In

相关标签:
7条回答
  • 2021-02-05 04:27
    • Put it inside an empty for() loop, and add last; everywhere you want to break out AND after the if. A bit ugly but works. Make sure to add comments to explain the trick.

      for (;;) {
          if (condition) { 
              #code
              last if another_condition;
          }
          last;
      }
      
    • use goto and label a statement after your loop for that goto. Be forever damned.

    • Extra block inside the if (e.g. if () {{ code }}). May be hard to read for novices but OK if accompanied by a comment.

    • your own solution: block around if. Not very obvious readability-wise.

    • your own solution: subroutine with return.

      Frankly, unless the cost of calling a sub matters performane wise, this is the cleanest solution as far as readability.

    0 讨论(0)
提交回复
热议问题