For instance, I need to match some text if $do eq \'b\'
.
If I run this code:
if (($do eq \'b\') && (/text/))
{
do stuff
}
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.
It would be
if ($do eq 'b' && /text/) {
do stuff
}
Yes - if $do eq 'b'
is false, /text/
won't be evaluated.
No, right part of condition is not evaluated when left part is false
,
if ($do eq 'b' && /text/)
{
do stuff
}
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.
Short answer: it stops ("short circuits" as AJ points out). You can get a ton more detail here.
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)