Broad Network


Boolean Logic and Perl Conditions

Perl Basics – Part 6

Perl Course

Foreword: In this part of the series we apply Boolean logic to Perl conditions.

By: Chrysanthus Date Published: 29 Mar 2015

Introduction

This is part 6 of my series, Perl Basics. In this part of the series we apply Boolean logic to Perl conditions. You should have read the previous parts of the series before reaching here, as this is a continuation.

Single Expression Example
Consider the following code:

use strict;

my $me = "tall";

if ($me eq "tall")
    {
        print 'I am tall';
    }

Try the code. eq means, equal to. In the condition (parentheses of if) there is only one expression, which is, $me eq "tall". Do not confuse between my and me. my is a reserved word and me preceded by $ in the code, is a variable (name). If this expression results in true, the if-block will be executed.

In Perl  the number 1 means true in a condition and the number 0 means false. In fact any number except 0 means true; but do not worry about that for now. The above if-statement is equivalent to

    if (1)
        {
            print 'I am tall';
        }

For this second if-statement to be executed, you do not need the creation of the variable and its assignment. Try the following code:

use strict;

my  $me = "tall";

    if (1)
        {
            print 'I am tall';
        }

Let us look at a case where the condition results in false. Consider the following code:

use strict;

my $me = "short";

if ($me eq "tall")
    {
        print 'I am tall';
    }

The if-block (curly braces) in the above code will not be executed, because the condition results in false, since the value of the variable, $me, is “short” and not “tall”. The above if-statement is equivalent to:

if (0)
    {
        print 'I am tall';
    }

The if-block can only be executed if the condition is true. In this last case it is not executed, since zero means false.

More than One Expression in Condition
You can have more than one expression in a condition. In this part of the series, I consider a maximum of two expressions in a condition. Each of the expressions results in true or false. The expressions are combined with the AND, OR or NOT operators. The AND operator is typed as, &&. The OR operator is typed as, || . The NOT Operator is typed as ! . &&, || , and ! are called logical operators. With logical operators, the rules in the previous part of the series can be rewritten as:

AND
(false) && (false) = false
(false) && (true) = false
(true) && (false) = false
(true) && (true) = true

OR
(false) || (false) = false
(false) || (true) = true
(true) || (false) = true
(true) || (true) = true

NOT
!(false) = true
!(true) = false

Double-Expression Examples
The if-block will not be executed in the following code:

use strict;

if ((0)&&(1))
    {
        print 'We are tall';
    }

A practical example for the above code is:

use strict;

my $you = "tall";
my $me = "tall";

if (($you eq "short")&&($me eq "tall"))
    {
        print 'We are tall';
    }

“tall” is assigned to the variable, $you, and also to the variable, $me. The first expression in the condition results in false and the second one results in true. (false)&&(true) gives false as the effective Boolean value for the condition. So the block is not executed.

The if-block will be executed in the following code:

use strict;

my $you = "tall";
my $me = "tall";

if ((0)||(1))
    {
        print 'Either of us is tall';
    }

A practical example for the above code is:

use strict;

my $you = "tall";
my $me = "tall";

if (($you == "short")||($me == "tall"))
    {
        print 'Either of us is tall';
    }

Read the above code. Try it. The first expression results in false; the second one results in true. The effective condition is true, since (false)||(true) gives true.

NOT Examples
The if-block will be executed in the following code:

use strict;

if (!(0))
    {
        print 'I am tall';
    }

The if-block is executed, if the condition is true. !(false) gives true.

A practical example for the above code is:

use strict;

my $me = "tall";

if (!($me eq "short"))
    {
        print 'I am tall';
    }

elsif and else
You can still add the elsif and else sub statements to the above code samples, following what we learned in one of the previous parts of the series.

Let us stop here and continue in the next part of the series.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
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
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message