#### Do you have a question? Post it now! No Registration Necessary. Now with pictures!

**posted on**

June 10, 2009, 3:48 pm

I exploit the flexibility of perl so

that as long as the scalars functions correctly

in a condition

if($scalar) { ...

I deem them "good".

Now, I would like to compare the two scalars,

to see if they match:

essentially:

$condition1 = getCondition1();

$condition2 = getCondition2();

if($condition1 == $condition2) { ...

But this doesn't do what I want. There's a wide

range of legal (and useful) values for the scalars,

and they might well be different, but all represent "truth"

or "falsehood".

So - what's a nice way of comparing the "truth" of two

scalars, as opposed to (just) comparing the scalars?

clearly:

sub bool {

my ($b) = @_;

return $b ? true : false;

}

if(bool($condition1) == bool($condition2)) { ...

works, but it's a bit of a sledgehammer to crack a nut.

Can anyone do better?

BugBear

## Re: compare two booleans for equality?

bugbear> So - what's a nice way of comparing the "truth" of two

bugbear> scalars, as opposed to (just) comparing the scalars?

if ($foo && $bar or !$foo && !$bar) probably makes your intent clearest.

if (!!$foo == !!$bar) is a trick I learned back in my C days.

Charlton

--

Charlton Wilbur

cwilbur@chromatico.net

## Re: compare two booleans for equality?

One way would be

unless ($condition1 xor $condition2) {

(it's always somewhat puzzled me that there's no ^^ operator...).

The standard idiom for boolifying is '!!'. So you could also use

if (!!$condition1 == !!$condition2) {

if you're unhappy with the idea of xors.

Ben

## Re: compare two booleans for equality?

There is xor though:

#!/usr/bin/perl

use strict;

use warnings;

print map { ($

___->[0] xor $___->[1]) ? 'T' : 'F' } (

[1, 1],

['sinan', time],

[42, 'T'],

[F => 0],

);

______END

______

*C:\Temp> z*

FFFT

--

(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:

http://www.rehabitation.com/clpmisc /

## Re: compare two booleans for equality?

^^^^^

One of us is very confused... :)

What has puzzled me is that while there are both bitwise '^' and low-prec

'xor' forms, there's no high-prec logical '^^' form. It just seems a

curious ommission. I guess at some point somebody thought that the

concept of 'logical XOR' was sufficiently obscure that it ought to be

written out in words.

Ben

## Re: compare two booleans for equality?

I wonder whether it was merely because C didn't have it? Or maybe

because || and && are short-circuited, but logical XOR cannot be, and

therefore ^ may have been seen as good enough when the operands are 0

and 1? (Has someone already mentioned (!operand1 ^ !operand2)?)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Tangent:

In Googling, I ran across this at <http://perlcabal.org/syn/S03.html ,

apparently describing Perl 6:

infix:<^^>, short-circuit exclusive-or

$a ^^ $b ^^ $c ...

Returns the true argument if there is one (and only one). Returns

Bool::False if all arguments are false or if more than one

argument is true. In list context forces a false return to mean

(). See xor below for low-precedence version.

This operator short-circuits in the sense that it does not

evaluate any arguments after a 2nd true result. Closely related is

the reduce operator:

[^^] a(), b(), c() ...

but note that reduce operators are not macros but ordinary list

operators, so c() is always called before the reduce is done.

What does "This operator short-circuits in the sense that it does not

evaluate any arguments after a 2nd true result." mean? In the

expression

$a XOR $b XOR $c XOR $d XOR $e

where XOR is my representation of the concept of logical XOR, you

can't determine the value of the expression without evaluating all 5

arguments.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Even more tangent:

I also ran across <http://www.ozonehouse.com/mark/periodic/ .

Periodic Table of the Operators

Being a comprehensive and complete enumeration of the Operatic

Elements of the Perl 6 Language, assembled and drawn with

dedication and diligence by M. Lentczner as a service to both the

Community and the Republic.

May this simple presentation with various illustrative devices

increase Knowledge & Understanding amongst practitioners in the

art of Software.

Third Edition, February 14th, Two Thousand Nine

OK, I didn't mind APL, but this makes me shudder. I am DEFINITELY

going to write in a small subset of Perl 6.

--

Tim McDaniel, tmcd@panix.com

## Re: compare two booleans for equality?

On Thu, 11 Jun 2009 20:42:06 +0000 (UTC), tmcd@panix.com (Tim McDaniel) wrote:

Is this irrelavent? Does this produce a new heretofore unknown

cpu op instruction? Shouldn't we expect our lang opperata's to mirror

the cpu at its core instructions without abreviations?

-sln

Is this irrelavent? Does this produce a new heretofore unknown

cpu op instruction? Shouldn't we expect our lang opperata's to mirror

the cpu at its core instructions without abreviations?

-sln

## Re: compare two booleans for equality?

Quoth tmcd@panix.com:

^^^^^^^

^^^^^^^^^^^ ^^^^^^^^^^^^^^^^

^^^^^^^^^^^^^^^^^

<snip>

According to the definition above, you can:

True XOR True XOR Any XOR Any

is False. Clearly you and Perl 6 have different idea about how XOR

associates :).

<snip>

[Perl 6's host of operators, most Unicode]

Well, yes. I rather doubt

***anyone***is going to write in more than a

small subset of Perl 6: it must be one of the 'largest' languages ever

designed.

Ben

## Re: compare two booleans for equality?

The loud

***SLAP***you heard from the direction of Austin was me slapping

my forehead. I am so VERY sorry that I didn't read that sentence.

It was ridiculously careless of me.

Is there another language that has such a definition?

Hrm! Does

A ^^ B ^^ C ^^ ... ^^ X

match any non-trivial parenthesization of the same expression?

I'm not used to normal-looking operators that can't be parenthesized.

For pure XOR, a binary operator that returns true only if its two

operands have different truth values, does it matter in what order you

evaluate a longer non-parenthesized expression?

--

Tim McDaniel, tmcd@panix.com

## Re: compare two booleans for equality?

No. XOR is a commutative and associative operator, just as OR and

AND. A sequence "b1 XOR b2 XOR b3 XOR ... XOR bn" evaluates to true

if and only if an odd number of the b's are true. This, of course,

implies that there is no mathematical sound way of short-circuiting

XOR; one needs to evaluate all b's in order to determine the parity of

the number of true values.

--

Rasmus Villemoes

<http://rasmusvillemoes.dk/

## Re: compare two booleans for equality?

meaning the Perl 6 meaning: return true iff exactly one operand is

true.

I wasin a hurry when I asked that; it took me only a little time to

see a counterexample. When $x = $y = $z = 1, $x ^^ $y ^^ $z is false.

If it were evaluated by parts, that is, parenthesized, two operands

would be ^^ed and then the result ^^ed with the third. 1 ^^ 1 is

false, and false ^^ 1 is true. So no parenthesizing can evaluate to

the correct value.

I just have to think of it as an n-ary operator.

--

Tim McDaniel, tmcd@panix.com

#### Site Timeline

- » FAQ 2.9 What are the Perl newsgroups on Usenet? Where do I post questions?
- — Next thread in » PERL Discussions

- » FAQ 3.1 How do I do (anything)?
- — Previous thread in » PERL Discussions

- » s suffix question
- — Newest thread in » PERL Discussions

- » Adblock Testscript problem
- — The site's Newest Thread. Posted in » HTML Markup Language