I've got some code I'm working with at the moment, that validates sections of XML, according to a DTD-like regex.

Sometimes the regex specifies bits I do want included (eg, dtd => '(open? #PCDATA? la #PCDATA? close?)+' collects our labels, and some attached text, delimeted by brackets.

Sometimes, however, it's easier to say that I want everything except certain pieces swept together: dtd => '(p | n | nqp)+', rev => 1 excludes paragraphs and notes from being included in new paragraphs.

The code I'm using to validate this is:

my @kids=$_->children; my $struct=' '.join (' ', map { $_->tag} @kids).' '; my $hit=$struct=~/^(?:$subelms{$tag}{'dtd_re'})$/; $loose=!$hit; $loose=!$loose if $subelms{$tag}{'rev'};
That is: build the string that represents the subelements, compare that to the specified dtd, and reverse the sense of the match if the rev attribute is set.

The last couple of lines could be recoded as $loose=!$hit xor $subelms{$tag}{'rev'} (and probably will be now you've reminded me of this.

Why don't I use xor more often? Well, if I'm dealing with only boolean values (0 and 1), then usually I find it clearer to use == or !=. If I'm not dealing with booleans, then I really don't want to touch xor.

--
Tommy
Too stupid to live.
Too stubborn to die.


In reply to Re: logical Exclusive Or, xor by tommyw
in thread logical Exclusive Or, xor by fruiture

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.