Perl Breaking out of an If statement

后端 未结 7 846
深忆病人
深忆病人 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:01

    I tend to use sequential if-statements based on a "do I continue?" variable instead. Your

    if ( $condition1 ) {
      blah, blah, blah;
      if ( not $condition2 ) {
         blah, blah, blah;
         if ( not $condition3 ) {
            blah, blah, blah;
         }
      }
    }
    

    can be rearranged to

    my $ok = $condition1;
    if ($ok) {
      blah, blah, blah;
      $ok = not $condition2;
    }
    if ($ok) {
      blah, blah, blah;
      $ok = not $condition3;
    }
    if ($ok) {
      blah, blah, blah;
    }
    

提交回复
热议问题