in reply to logical Exclusive Or, xor
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:
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.my @kids=$_->children; my $struct=' '.join (' ', map { $_->tag} @kids).' '; my $hit=$struct=~/^(?:$subelms{$tag}{'dtd_re'})$/; $loose=!$hit; $loose=!$loose if $subelms{$tag}{'rev'};
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.
|
|---|