I have used 'xor' operator as lower precedence comma
, because I understood a logic that it cannot short-circuit as do 'or' and 'and', and the last expression must be evaluated at the end. E.g. I used 'xor' in short sentences like this, to avoid unnecessary parentheses:
push @_, $_ xor print $_ for 'a' .. 'b';
Useless use of logical xor in void context at ./perlmonks_xor_vs_or_an
+d.pl line 6.
Today I found (for myself) that 'xor' doesn't work in the same way as 'or' and 'and' do, and it is strange for me, why. It seems that, 'xor' not only evaluates one or both expressions surrounding it, but also evaluates logical XOR of these expressions, when 'or' and 'and' operators don't do this, i.e.:
print "[$_]" for
( 2 and 3 ),
( 2 or 3 ),
( 2 xor 3 ),
At the last line of output I expected to get '3
I could get '3' by using a comma instead (and forcing scalar context): 'scalar( 2, 3 ),
I'm not sure this difference of behaviour of operators is written somewhere in documentation, but I haven't find it in 'perlop'.