Not that I have anything really earth-shattering to add to what others have written, but just wanted to add another voice to the 'xor can be useful' crowd...

I used xor in the following situation:

There are 3 states, call them A, B, and Z, where the first 2 are sort of similar, and the last is different. I needed a conditional to test for the transition between (A or B) and Z (or vice versa). So instead of writing some monstrous thing like:

if ((($before eq 'A' || $before eq 'B') && $after eq 'Z') || ($before eq 'Z' && ($after eq 'A' || $after eq 'B'))) { # change from A|B <=> Z }
I simply wrote this:
if ($before eq 'Z' xor $after eq 'Z') { # change from A|B <=> Z }

I hope my explanation made sense... but that's my story of how I used xor...

Update: Upon further reflection, my non-xor conditional would be more simply and accurately written as one of these:

if (($before ne 'Z' && $after eq 'Z') || ($before eq 'Z' && $after ne 'Z')) { # change from A|B <=> Z } if (($before eq 'Z' || $after eq 'Z') && !($before eq 'Z' && $after eq 'Z')) { # change from A|B <=> Z }

... and that is equivalent to Abigail-II's summary of xor's logical equivalency:
if ((COND1 || COND2) && !(COND1 && COND2))

But I think it's still clear why xor is useful here, and perhaps I shed some light on a real-world usage... Not having looked at the source, but only bsb's post, I am guessing it's similar to the situation here:

/usr/local/lib/perl/5.6.1/DateTime.pm: if ( $self->{tz}->is_floatin +g xor $was_floating )

--
3dan

In reply to Re: Anyone use "xor" in conditionals? by edan
in thread Anyone use "xor" in conditionals? by bsb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.