The following Perl program prints the truth table for a Boolean function of two variables:
# print "true" or "false" for a Boolean value
sub printBool {
my($val) = @_;
if ($val) { print 'true '; }
else { print 'false'; }
}
# calculate a Boolean function of two variables
sub BoolFun {
my( $v1, $v2 ) = @_;
return !( $v1 && $v2 );
}
# print a header
print " b1 b2 f(b1, b2)\n";
print " ----- ----- --------------\n";
for ($b1=0; $b1<=1; $b1++) {
for ($b2=0; $b2<=1; $b2++) {
print " ";
printBool( $b1 ); # print the first variable
print " ";
printBool( $b2 ); # print the second variable
print " ";
printBool( BoolFun( $b1, $b2 ) ); # print the result
print "\n";
}
}