Is Perl optimized to skip remaining logic operands if the answer is already decided?

后端 未结 6 1734
悲&欢浪女
悲&欢浪女 2020-12-21 04:22

For instance, I need to match some text if $do eq \'b\'. If I run this code:

if (($do eq \'b\') && (/text/))
{
do stuff
}
相关标签:
6条回答
  • 2020-12-21 04:52

    Yes. This behavior is called short-circuiting in the perlop documentation (emphasis added).

    C-style Logical And

    Binary && performs a short-circuit logical AND operation. That is, if the left operand is false, the right operand is not even evaluated. Scalar or list context propagates down to the right operand if it is evaluated.

    C-style Logical Or

    Binary || performs a short-circuit logical OR operation. That is, if the left operand is true, the right operand is not even evaluated. Scalar or list context propagates down to the right operand if it is evaluated.

    In addition to && and || their lower-precedence cousins and and or also short-circuit.

    Perl has an xor operator, but it cannot short-circuit due to the definition of exclusive-or.

    0 讨论(0)
  • 2020-12-21 04:55

    It would be

    if ($do eq 'b' && /text/) {
        do stuff
    }
    

    Yes - if $do eq 'b' is false, /text/ won't be evaluated.

    0 讨论(0)
  • 2020-12-21 05:02

    No, right part of condition is not evaluated when left part is false,

    if ($do eq 'b' && /text/)
    {
    do stuff
    }
    
    0 讨论(0)
  • 2020-12-21 05:04

    Yes. This behavior is commonly used in idiomatic perl.

    Consider:

    open FILE, ">$fname" or die $!;
    

    If there is no error opening the file (open returns true), the die statement is not executed. This is not special treatment of the die routine; it is just the way perl handles logic evaluation.

    0 讨论(0)
  • 2020-12-21 05:11

    Short answer: it stops ("short circuits" as AJ points out). You can get a ton more detail here.

    0 讨论(0)
  • 2020-12-21 05:13

    Yes, Perl short circuits logical operators. This means they can be used for control flow:

    say "foo" if $bar;
    

    is the same as

    $bar and say "foo";
    

    and

    if ($cond) { stuff() }
    

    is the same as

    $cond and do { stuff() }
    

    However, the whole condition of an if has to be wrapped in parens, making your example

    if ($do eq 'b' && /text/) {
      do stuff;
    }
    

    (&& is the same as and, but has higher precedence)

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