Robert's Perl Tutorial

http://www.sthomas.net/roberts-perl-tutorial.htm


Other Logical Operators

Perl has not , which works like ! except for low precedence. If you are wondering where you have seen ! before, what about:

$x !~/match/;

if ($t != 5) {

as two examples. There is also Exclusive OR, or XOR. This means:

This needs an example. Jane and Sonia are two known troublemakers, with a reputation for throwing good beer around, going topless at inappropriate moments and singing out of tune to the karaoke machine. You only want to let one of them into your party, and instead of a big muscle-bound bouncer you have this perl script on the door:

($name1,$name2)=@ARGV;

if ($name1 eq 'Jane' xor $name2 eq 'Sonia') {
	print "OK, allowed\n";
} else {
	print "Sorry, not allowed\n";
}

I would suggest running it thus:

perl script.pl Jane Karen (one true, one false)
perl script.pl Jim Sonia (one true, one false)
perl script.pl Jane Sonia (both true)
perl script.pl Jim Sam (both false)
   

Well, the script is not perfect as a doorman, as all Jane and Sonia have to do is type their names in lowercase, but hopefully it demonstrated xor .

One thing to beware of is:

$_=shift;

print "OK\n" unless not(!/r/i || /o/i & /p/ or /q/);

over-complication, and believe me the above is not as complicated as it could be. Take the time to understand what you want to do. Perl provides a plethora of logical operands so you really don't have any excuse for not writing legible code. The above can be written a lot more concisely and clearly. As well as a lot more obscurely :-)

@ARGV