Robert's Perl Tutorial

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


And

now for something similar. And. Logical AND operators evaluate two expressions, and return true only if both are true. Contrast this with OR, which returns true only of one or more of the two expressions are true. Perl has a few AND operators.

The first type of AND we will look at is && :

@list=qw(a b c);

print "List is:@list\n";

if ($list[0] eq 'x' && $list[2]++ eq 'd') {
	print "True\n";
	} else {
	print "False\n";
}

print "List is:@list\n";

The output here is false. It is clear that $list[0] does not equal x . As AND statements can only return true if both expressions being evaluated are true, then as the first statement is false this is an obvious non-starter and perl decides it need not continue to the second statement. Entirely sensible.

The second type of AND statement is & . This is similar to && . See if you can work out what the difference is using this example:

@list=qw(a b c);

print "List is:@list\n";

if ($list[0] eq 'x' & $list[2]++ eq 'd') {
	print "True\n";
	} else {
	print "False\n";
}

print "List is:@list\n";

The difference is that the second part of the expression is evaluated no matter what the result of the first part is. Despite the fact that the AND statement cannot possibly return true, perl goes ahead and evaluates the second part of the statement anyway, hence $list[2] ends up as d .

The third AND which we will look at is and . This behaves in the same way as && but is lower precedence. Therefore, all the guidelines about || and or apply.