I'm sorry Paul. I am new to Perl, and my understanding is
somewhat limited, and I feel that the documentation is letting
me down here.
No apologies are necessary, I'm sorry if I implied such. I'm just
confused because I thought this was already explained previously in
this thread. Perhaps not.
Anyhow, I am trying to follow this.
You are saying || returns the first true value.
Yes. Because in order for a chain of arguments connected by || to be
true, only one of those values has to be true. Thus, as soon as we
come to the first true one, Perl can stop looking at the rest. This
is known as short circuit evaluation.
print (2 || 7); # 7, both true, but the first value is 2,
No it doesn't, unless you have some very old or obscure version of
Perl installed:
$ perl -le'print (2 || 7);'
2
$
When you run that, it produces 7? Really? If so, please copy and
paste the output of this:
perl -v
print ("apples" || "pears");
# apples, both true, but why return "apples"?
Because as soon as "apples" was found to be true (all strings other
than "" and "0" are true), there was no need to look at the other
arguments. Perl never even saw "pears" in this evaluation. As soon
as it found one argument to be true, the whole expression is true.
I am still not yet clear on why the numerical one, returns the
opposite argument.
It doesn't. Please check that again.
I think the issue is that I am not able to foresee the behaviour
from the either the perlop manual (or certainly not from the page
I was looking at, or from my expensive Professional Perl
Programming book.
There's really only a couple things you have to make sure you
understand, for this to be clear: The operators (and, or, &&, ||) all
use short circuit evaluation. As soon as it's possible to determine
the results of the entire expression, the operator returns. And they
always return the last value evaluated. For || or or to be true, only
one of the chain of values need be true. For || or or to be false,
they all must be false. The opposite is true for && or and: to be
true, they must all be true. To be false, they must all be false.
If all values are true, || returns the first one, && returns the last:
$x = 1 || 2 || 3 || 4; #1
$x = 1 && 2 && 3 && 4; #4
If all values are false, || returns the last one, && returns the
first:
$x = 0 || "0" || undef || ''; #''
$y = 0 && "0" && undef && ''; #0
If there is a mixture, || returns the first true value, && returns the
first false value:
$x = 0 || 1 || undef || 5; #1
$x = 0 && 1 && undef && 5; #0
Looking at &&, you are saying first false, or last value.
That is what I am getting:
print (2 && 7); # 7, Ok, that matches my understanding
of your theory
It's not a theory, it's simply what is.
print ("apples" && "pears"); # pears, Ok, so does that
Excellent, thanks for that! (Unless I understand for a wrong
reason, maybe, or maybe don't understand but, think I do, Huh!)
Nope, you've got it.
I am still having a problem with XOR:
print ("apples" xor "pears"); s (true), incorrect, should be 0
(false)
Again, it doesn't return true for me. I don't know what's making you
think it does. For me, it returns the empty string, which is false:
$ perl -le'print ("apples" xor "pears");'
$
With "apples" and "pears" both being true, I was expecting a
result of false here.
Yes, and that's exactly what it produces. What output are you seeing
that contradicts that? Please copy and paste your actual code and
output, rather than typing in a comment what you think you're seeing.
Paul Lalli