Perl Breaking out of an If statement

后端 未结 7 803
深忆病人
深忆病人 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;
    }
    
    0 讨论(0)
  • 2021-02-05 04:02

    You could put the rest of your if block inside another if statement, like this:

    if (some_condition) {
        blah, blah, blah
        if (!$some_other_condition) {
            blah, blah, blah
            ...
        }
    }
    
    0 讨论(0)
  • 2021-02-05 04:06

    I was inspired by DVK's answer to play around, and I came up with this variant that works at least on Perl 5.26.1:

    for( ; some_condition ; last ) {
        blah, blah, blah
        last if $some_other_condition; # No need to continue...
        blah, blah, blah    
    }
    

    Per perlsyn, this is equivalent to:

    while (some_condition) {
        blah, blah, blah
        last if $some_other_condition; # No need to continue...
        blah, blah, blah    
    } continue {
        last;
    }
    

    In a continue block, last has the same effect as if it had been executed in the main loop. Therefore, the loop will execute zero or one times, depending on some_condition.

    Tests

    perl -E 'my ($cond, $other)=(X, Y); 
             for(;$cond;last) { say "hello"; last if $other; say "goodbye" }'
    

    has the following results, for various X and Y values:

    X Y   Prints
    -----------------------
    0 0   (nothing)
    0 1   (nothing)
    1 0   hello, goodbye
    1 1   hello
    
    0 讨论(0)
  • 2021-02-05 04:19

    Another alternative is to use an anonymous subroutine.
    Note: I don't recommend this method because of the added scoping complexity (see note below); it is just for completeness of possible answers.

    if( $true_condition ){
       (sub{
           return if $true_condition;
           ...
       })->();
    }
    

    Note: any variables declared w/in the routine must use our instead of my if you wish to use them in the rest of the code.

    0 讨论(0)
  • 2021-02-05 04:20

    You can use basic block which is subject to last, next and redo, so there is possible break from it.

    if ($condition) {EXIT_IF:{
    
       last EXIT_IF; # break from code block
    
       print "never get's executed\n";
    }}
    

    EXIT_IF: {
      if ($condition) {
    
         last EXIT_IF; # break from code block
    
         print "never get's executed\n";
      }
    }
    
    0 讨论(0)
  • 2021-02-05 04:20

    Keep your while loop so you can use last but also make sure that the loop is executed at most once

    my $loop_once = 1;
    while ( $loop_once-- and some_condition ) {
        blah, blah, blah
        last if $some_other_condition; # No need to continue...
        blah, blah, blah
        ...
    }
    
    0 讨论(0)
提交回复
热议问题