http://qs321.pair.com?node_id=11110345

rsFalse has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

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:
#!/usr/bin/perl -l use strict; use warnings; push @_, $_ xor print $_ for 'a' .. 'b'; print @_;
Output:
Useless use of logical xor in void context at ./perlmonks_xor_vs_or_an +d.pl line 6. a b ab
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.:
#!/usr/bin/perl -l use strict; use warnings; print "[$_]" for ( 2 and 3 ), ( 2 or 3 ), ( 2 xor 3 ), ;
Output:
[3] [2] []
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'.