Symbolic representation of logical operators

P

Paul Lalli

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.

ACK! That should read:
The opposite is true for && or and: to be true, they must all be
true. To be false, only one need be false.

Sorry about that!
Paul Lalli
 
M

Michele Dondi

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");'

$


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.

(To the OP) to be sure, try print()ing

"apples" xor "pears" ? 'TRUE' : 'FALSE'


Michele
 
J

jgraber

Paul Lalli said:
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

This is likely due to a typo on OP part, of | vs ||.
perl -le 'print (2||7)' # prints 2, logical or
perl -le 'print (2|7)' # prints 7, bitwise num or: 0010b | 0111b = 0111b
perl -le 'print (2|8)' # prints 10, bitwise num or: 0010b | 1000b = 1010b
also note:
perl -le 'print ("2"|"7")' # prints 7, bitwise ascii or:
# 00110010b = '2'
# | 00110111b = '7' = 00110111b = '7'
perl -le 'print ("2"|"8")' # prints :, bitwise ascii or:
# 00110010b = '2'
# | 00111000b = '7' = 00111010b = ':'
 
B

Ben Morrow

Quoth Michele Dondi said:
(To the OP) to be sure, try print()ing

"apples" xor "pears" ? 'TRUE' : 'FALSE'

ITYM

("apples" xor "pears") ? 'TRUE' : 'FALSE'

And the OP needs to realise that to print this one must use

print( ("apples" xor "pears") ? 'TRUE' : 'FALSE' );

due to Perl's 'if it looks like a function it parses as a function'
rule.

Ben
 
T

Tad McClellan

Michele Dondi said:
Yep, sorry!


Or

print +("apples" xor "pears") ? 'TRUE' : 'FALSE';

which I like and confuses me less, but maybe that's just me. Come to
think of it, I must have never used C<xor>.


The rule is a consequence of having optional parenthesis around
a function's argument list.

See also:

Message-Id: <[email protected]>


The least confusing "fix" is to use parenthesis around the
function's argument list, even if you don't normally use them:

print(("apples" xor "pears") ? 'TRUE' : 'FALSE');
 
M

Michele Dondi

The least confusing "fix" is to use parenthesis around the
function's argument list, even if you don't normally use them:

print(("apples" xor "pears") ? 'TRUE' : 'FALSE');

Confusion is in the eye of the beholder.


Michele
 
P

Peter J. Holzer

print (7 xor 2); # Empty string, I was expecting 0 (true xor true = false)

The empty string is also false, and all operators which return a "pure"
truth value (==, !=, <, >, <=, >=, !) return 1 as true and an empty
string as false. So xor is consistent with the rest of perl (although
not consistent with any other programming language I know).

The empty string returned by these operators is actually quite an
interesting piece: It is both an empty string and the number 0 at the
same time:

#!/usr/bin/perl
use warnings;
use strict;

my $x = "";
print "\$x=<$x>\n"; # Prints "<>"
my $y = $x + 5; # Warning: Argument "" isn't numeric in addition (+)

$x = !"b";
print "\$x=<$x>\n"; # Prints "<>"
$y = $x + 5; # No warning

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,222
Latest member
patricajohnson51

Latest Threads

Top