In this video, you'll cover the two types of logical operators.
- [Voiceover] Logical operators are used to perform Boolean operations on logical values. Here's a working copy of relational.pl from chapter eight of the exercise files. Perl has two sets of logical operators for two different purposes. Low precedence logical operators are used to bind logical expressions according to Boolean rules. For example, we have this conditional expression here that we used earlier in this chapter, and if I run this you see it says false, because a is not actually equal to b, a is a seven, and b is 42.
Now if I come down here and I say if a equals b, or rather, if a equals c, and $b is equal to $d, we notice that a is equal to c, and b is equal to d, so we should get true. And I'll run that, and you see that the result is true. So this and, right there, is a logical operator. It's very low priority, and so the expressions on either side of it are always going to be evaluated first.
And this makes it very useful for this sort of usage. Now, just so that we can look at the logical operators, I'm going to go ahead and define a couple of constants for true and false. So I'm going to say use constant, true is one, and false, make that capital, is an empty string. These are the traditional definitions of true and false in Perl. And now I can just say if true and false, and we know that this is going to have a false result, because true is not equal to false.
And so, logically, true and true would be true. I'll save that and run it, and we get true. Or false and false which is false, because neither of them is true. So false and false is false. The only and would have to have true on both sides. So true and true is true. Great. Okay, we also have or. So I can say true or true is, of course, going to be true, and true or false is going to be true, because the or expression, the boolean or, is true if either of the two arguments are true.
We also have not. So if I say not true, now that left expression is going to be false because not will invert it. It'll take a true and make it false, it'll make a false and make it true, so when I run this it's now false, because neither of these is true. And if I put a not on this side then it becomes true again. I can also use xor, which is the exclusive or, which requires that one side is true and the other side is false, and so it's true in this case.
If I make this one true, now it's false. And if I make this one false, now the result is true. So those are our Boolean operators. These are very low priority, and they're designed for comparing the results of expressions. Because of the low priority of these operators, you'll find them at the bottom of your operator precedence chart. Because of their very low precedence, they'll always allow the expressions on either side of the operator to evaluate first.
There are also higher precedence logical operators that are used to create for entirely difference of purpose. They're used for short-circuits. And these operators use the traditional symbols, so double ampersand for and, and of course this will evaluate to false, but it'll evaluate to true if I make this one true. And the double vertical bar which is used for the or operator. And this will evaluate to true even if one of these is false, but not if both of them are.
But that's not what both of these operators are normally used for. These operators are normally used for short-circuiting an expression, an assignment, in particular. So, for example, if I say my $x is equal to a or 57, and then I'll say $x. Now when I run this I get a seven, because a is equal to seven. But if I make a up here equal to zero, now x will be 57 because it's evaluating this first expression, and if it's true it goes ahead and uses it, but if it's not true then this or allows the other side of the expression to be evaluated.
So, the other side of the expression is only evaluated if it needs to be evaluated, and then that value is returned. So this is really useful for or, it's not so useful for and, and it's not so useful for not. There's actually three of these. If I go ahead and make this a seven again I'll show you the not. So now our result is seven, but if I put an exclamation point in front of this that's the symbolic not. Now we get 57, because a is no longer true, and so the or is evaluated, and the 57 is assigned.
Now, in general, the named logical operators are more useful for logical conditions, and the symbolic operators are more useful for these short-circuit assignments. But you can still get confusing results if you don't understand the precedence of the operators. So when in doubt, you can consult your precedence chart. It's always good practice to use parentheses, and to experiment until you understand it.
Watch to learn the details of the Perl syntax, from variables, conditionals, loops, and data structures to regular expressions, functions, and references. A quick-start guide is included for experienced developers who want to get up and running with Perl 5 fast, and the entire course is recommended for both new and experienced programmers alike. Later chapters cover file handling and reusing code with Perl modules, plus Perl best coding practices.
- Understanding Perl's general syntax and the anatomy of a Perl script
- Writing statements and expressions
- Creating assignments
- Working with variables and strings
- Using data types effectively
- Defining logical flow with conditionals and loops
- Using special variables
- Using Perl operators
- Performing simple Perl programming tasks with expressions
- Matching data
- Defining and calling functions
- Using references
- Handling files in the file I/O
- Using built-in functions
- Reusing code with modules
- Coding with Perl best practices