Perl Conditional Statements
Perl Basics – Part 4
Perl Course
Foreword: In this part of the series, I explain how a group of statements can be executed based on a condition.
By: Chrysanthus Date Published: 29 Mar 2015
Introduction
The if Construct
In Perl, there is a reserved word, which is “if”. The “if” must be in lowercase. This is used to check if a condition is true. If it is true, one or more statements are executed. I present an example. Consider the following code:
use strict;
my $hisVar = 20;
if ($hisVar == 20)
{
print "I am studying Perl, which has been optimized.";
}
You are advised to always start your script, with the statement, “use strict;”
In the code, you have the statement that assigns the value 20 to the variable, $hisVar. Remember, we are using $hisVar for the first time, so we have to begin with, my. Then you have the “if” construct. The if-construct begins with the reserved word, “if” and ends with the curly brace, }. Here, there is no semicolon after, }. What goes inside the parentheses is the condition. The statements to be executed are in the braces (curly brackets). The if-construct is not an ordinary statement, so it does not end with a semicolon. The whole if-construct (with the parentheses and curly braces) can be called the if-statement.
If the condition is correct, Perl will replace it with, true, internally; you do not see that. If it is wrong, Perl will replace it with, false, internally.
In the above code, 20 was assigned to, $hisVar. So, $hisVar equals 20. In the condition the equal sign is two assignment operators: one just next to the other. The if-construct above can be read like this: if $hisVar equals 20 then display, "I am studying Perl, which has been optimized.". Since we assigned the value 20 to $hisVar, the condition of the if-construct is true. So the statement in the curly braces is executed. Try the above code (you have to give it any file name with the extension .pl and save it in the root directory).
You can have more than one statement in the curly braces of the if-construct. If the condition is true, all the statements in the curly braces will be executed.
else
In the above code, the statement(s) in the curly braces is(are) executed if the condition is true. What if it were false? It would be false if we never assigned 20 to $hisVar. If it were false, nothing will happen; that is, the statement(s) in the curly braces will not be executed. There is an else sub construct you can attach to the if-statement. The else part is similar in coding to the if part. However, its block (curly braces) is executed when the if’s condition is false. The else part does not have any condition. Try the following code:
use strict;
my $hisVar = 36;
if ($hisVar == 20)
{
print 'I am studying Perl';
}
else
{
print 'I am doing something else';
}
In this code, a value of 36 is assigned to $hisVar. In the if-condition, we test if $hisVar is equal to 20. So the condition returns false, and the statement(s) in the else block is (are) executed. Note how the else section has been typed. Also note that else is a reserved word.
You may have more than one test to make in a particular situation or for the same variable. In this case you include the “elsif” reserved word as in the following code. Read and try it.
use strict;
my $hisVar = 1000;
if ($hisVar == 10)
{
print 'Value is small';
}
elsif ($hisVar == 100)
{
print 'Value is medium';
}
elsif ($hisVar == 1000)
{
print 'Value is large';
}
A value of 1000 is assigned to hisVar. The if-elsif coding will test if $hisVar is 10; if it is (which it is not) the corresponding block will display 'Value is small'. The code will then test if $hisVar is 100; if it is (which it is not), the corresponding block will display, 'Value is medium'. The code will then test if $hisVar is 1000; if it is, the corresponding block will display, 'Value is large'. With the if-elsif coding, only one of the blocks can be executed; that is, only one of the conditions can be true (the rest should be false).
In the if-elsif construct, the very first line must be the if-condition; the rest are elsif conditions. The elsif reserved word takes a condition, but the else reserved word never takes a condition.
Note that the reserved word is elseif and not elseif. There is no e between s and i.
Always remember this: the if-elsif construct is used only for situations where only one of the conditions is satisfied (is true). At the limit, no condition may be satisfied (no condition may be true).
elsif is a contraction of “else if”.
Default Condition
What about the situation for an if-elsif coding where none of the conditions is true? For that situation you will need to report (inform the user) something. This is an opportunity to give some default answer. You do this by simply adding the else (no condition) section at the end of the if-elsif construct. The following code illustrates this:
use strict;
my $hisVar = 10000;
if ($hisVar == 10)
{
print 'Value is small';
}
elsif ($hisVar == 100)
{
print 'Value is medium';
}
elsif ($hisVar == 1000)
{
print 'Value is large';
}
else
{
print '$hisVar is very large';
}
Try the above code, if you have not already done so. At the start of the code, 10,000 is assigned to the variable. Note that when you are applying numbers with more than 3 digits, you do not use commas (you type 10000 and not 10,000). In the code, none of the conditions is satisfied, so the last block, which does not have any condition (which is the else part), is executed. Read through the code again, to appreciate this.
The complete syntax for the if-statement is:
if (condition)
{
statements
}
elsif (condition)
{
statements
}
elsif (condition)
{
statements
}
- - -
else
{
statements
}
The switch Statement
The previous code may be replaced by the following. Read and try it.
use strict;
my $hisVar = 10000;
use feature "switch";
given ($hisVar)
{
when (10)
{
print 'Value is small';
}
when (100)
{
print 'Value is medium';
}
when (1000)
{
print 'Value is large';
}
default
{
print '$hisVar is very large';
}
}
The syntax for the switch statement is:
use feature "switch";
given ($variable)
{
when (value)
{
statements;
}
when (value)
{
statements;
}
- - -
default
{
statements;
}
}
You begin with the “use feature "switch";” statement. Then you have the given construct. It is not an ordinary statement. The last situation is the else situation. You use the switch statement instead of the if-elsif statement, when you want to compare the same variable with many different values.
However, the switch statement is still at the experimental level, so do not use it in your commercial project for now. For now, use the if-elsif-else construct in your commercial project
If your value is a number in the condition, you do not need to have it in quotes. However, if it is a string, you need to have it in quotes.
Case Sensitivity
Perl is case sensitive. So, the reserved words, if, else, elsif, given, when and default, must be typed in lowercase.
Let us stop here and continue in the next part of the series. As you continue, always remember that ActivePerl and other Perl interpreters are essentially the same things, but they work for different operating systems.
Chrys
Related Links
Perl BasicsPerl 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