itnifl
asked on
Perl: How does Perl handle multiple comparisons in one if sentence?
If(A<B && C>D && E=F) {
blabla
}
In C#, if C>D returns false then E=F will never get evaluated.
In Visual Basic Scripting all parts of the if statement get evaluated even if C>D fails.
How does Perl behave in this regard?
blabla
}
In C#, if C>D returns false then E=F will never get evaluated.
In Visual Basic Scripting all parts of the if statement get evaluated even if C>D fails.
How does Perl behave in this regard?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Code will only execute if the conditions in parentheses are met. Consider the following example. You will get no result because the condition in the second parentheses does not meet.
$a=1;
$b=2;
$c=3;
if(($a<$b) && ($b>$c)){
print "Perl is better.";
}
On this example, all statements inside the parentheses are met, and you will get your result - perl is better.$a=1;
$b=2;
$c=3;
if(($a<$b) && ($c>$b) && ($b==$b)){
print "Perl is better";
}
ASKER
farzanj:
Yes - from your link:
"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."
Tintin:
Yes it matters. I would rather write:
if(defined($string) && $string ne "")
If both expressions were evaluated even if the first one was false, I would get this error if $string was not defined:
"Use of uninitialized value $string in string ne at ./script.pl line xxx"
I then would be forced to write:
if(defined($string)) {
if($string ne "") {
}
}
This is not very elegant and adds bloat to my code.
I realize this example is a bit odd, why would I both check if a string is defined and if it is empty? The scenario is still valid in many other cases. I don't want other code to be executed in the if logical check if the first check(s) fail, because the code might not then be valid.
Mazdajai:
You are misunderstanding, read the questiin again :)
Yes - from your link:
"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."
Tintin:
Yes it matters. I would rather write:
if(defined($string) && $string ne "")
If both expressions were evaluated even if the first one was false, I would get this error if $string was not defined:
"Use of uninitialized value $string in string ne at ./script.pl line xxx"
I then would be forced to write:
if(defined($string)) {
if($string ne "") {
}
}
This is not very elegant and adds bloat to my code.
I realize this example is a bit odd, why would I both check if a string is defined and if it is empty? The scenario is still valid in many other cases. I don't want other code to be executed in the if logical check if the first check(s) fail, because the code might not then be valid.
Mazdajai:
You are misunderstanding, read the questiin again :)
I'm not familiar with the Perl internals to know how it evaluates comparisons, but given the result is going to be the same in any programming language, why does it matter?