in reply to In what order is a compound IF evaluated?

Perl evaluates left to right, but you need to be careful about operator precedence. (order of operations)

Like in math: 5 + 7 * 3
is evaluated as: 5 + (7*3) = 5 + 21 = 26
Not: (5 + 7) * 3 = 12 * 3 = 36
This is because multiplication has a higher precedence than addition, so sometimes you need parenthesis

As for not evaluating a the expressions, it depends on the compound expression. For instance:
my $x = 3; my $y = 5; # Case 1 if ( ($x > 1) || ($y > 10) ) {...} # Case 2 if ( ($x > 10) || ($y > 10) ) {...} # Case 3 if ( ($x > 10) && ($y > 10) ) {...} # Case 4 if ( ($x > 1) && ($y > 10) ) {...}
Case 1 will stop after the first part because True OR <Anything> is true

Case 2 will continue after the first part evals to false because False OR <Something> evals to True is the Something is true

Case 3 will stop after the first part evals to false because False AND <Anything> is false.

Case 4 will evaluate both parts because the first one is true, but True And False is False, so the second part needs to be evaluated.

So it really depends on how the parts are joined. This is extensible to 3, 4, ..., N clauses too

Replies are listed 'Best First'.
Re^2: In what order is a compound IF evaluated?
by jonadab (Parson) on Jul 10, 2004 at 14:10 UTC
    Perl evaluates left to right

    While it's certainly true that the short-circuit operators in Perl are left-to-right, which is what the OP mostly wanted to know at the moment, it might be saying to much to say that Perl evaluates left-to-right. It depends on your operators...

    print for map { $$_[0] } sort { lc $a->{name} cmp lc $b->{name} } grep { ref $_ } map { [$_, dblookup($_)] } <STDIN> unless grep { /--ignorestdin/ } @ARGV;

    Admittedly, the above is _somewhat_ atypical code.


    ;$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$;[-1]->();print