Boolean Logic for Perl
Perl Basics – Part 5
Foreword: In this part of the series I teach you a way of reasoning called Boolean Logic.
By: Chrysanthus Date Published: 29 Mar 2015
Assume that I am tall and you are tall. If someone ask you, if both of us are tall, you would say, Yes (true). If he asks if both of us are short, you would say, No (false). If you are short and I am tall, and he asks you if either you or me is tall, your answer would be Yes (true). If he asks if both you and me are tall, you would not have an answer. You might go on to say that the last question should not be asked or that the question does not have an answer. Well, I want you to know as from today that under certain circumstances the question should be asked.
In biology, a person is either tall or short. It is “environmental” conditions that make the person to have a medium height. One scientist, George Boole defined a set of answers or rules for this kind of questions. We learn this rules in this part of the series. These rules are used in computing, programming, electronics and telecommunications, today. In fact, without these rules, you will not have a computer; you will not also have programming.
True or False
A simple human language statement is either true or false in itself. If I say, “I am tall”, that is either true or false. If I say, “you are tall”, that is either true or false. If I am tall and you are short, and the question is asked, if both you and me are tall, in Boolean Logic, an answer of true or false must be given. Which of these two should be given? Boole did not really answer this question. He simply came up with a set of rules for us to follow. The good news is that, when you follow these rules in their right context, you do not have any ambiguity. Thanks to these rules, we have computers and programming, today. I will give you the rules. The rules cannot really be explained; you just accept them. The rules are under three headings: AND, OR and NOT.
The question can be asked, if both you AND me are tall. My height and your height are combined by AND. These are the rules to follow for AND:
false AND false = false
false AND true = false
true AND false = false
true AND true = true
Now, let tall be true and short be false. This means that if I am short AND you are short, you and me are short. If I am short AND you are tall, you and me are short; that is Boolean answer, which you have to accept. If I am tall AND you are short, both you and me are short. If I am tall AND you are tall, you and me are tall. All these are Boolean rules that you have to accept.
The question can be asked, if you OR me is tall. My height and your height are combined by OR. These are the rules to follow for OR:
false OR false = false
false OR true = true
true OR false = true
true OR true = true
Again, let tall be true and short be false. This means that if I am short OR you are short, you or me is short. If I am short OR you are tall, you or me is tall. If I am tall OR you are short, you OR me is tall. If I am tall OR you are tall, you or me is tall. All these are Boolean rules that you have to accept.
Now, in Boolean logic, only two states (possible answers) exist. That is, if you are NOT tall, you are short; if you are NOT short you are tall; nothing else. These are the rules to follow:
NOT false = true
NOT true = false
Assume that you have a string (spring), you can extend (pull). While the string is in its natural state, if I say, “NOT short” you would extend it; that is the interpretation. The extent to which the string is extended, does not matter here. While the string is extended, if I say, “NOT long” you would allow it to contract to its natural state; that is the interpretation.
You have to memorize all the above rules under their different categories of AND, OR, and NOT.
In Perl AND, OR and NOT are each called an operator. For the NOT operator, you need only one variable to have an answer. For AND or OR, you can have more than two variables. The above cases show two variables. You can have three variables for AND; something like:
false AND false AND false = false
false AND false AND true = false
These are two lines. There are actually nine lines when the variables are three (for AND or OR). When the variables are four, there are 16 lines.
You should consult some other text to know the pattern when the number of variables is higher than 2. It will not be difficult to identify the patterns. The pattern for AND and the pattern for OR are different.
True or false is actually a value and not a variable. I have referred to them as variables in this article, for simplicity.
I hope you now know what Boolean values (true or false) mean. Let us stop here, and carry on in the next part of the series.
Related LinksPerl Basics
Perl Data Types
Perl References Optimized
Handling Files and Directories in Perl
Perl Object Oriented Programming
Perl Regular Expressions
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library