ActivePerl Conditional Statements

Basics of ActivePerl - Part 4

Chrys Forcha
Introduction
This is part 4 of my series, Basics of ActivePerl. In this part of the series, we see how a group of statements can be executed based on a condition. It is similar to what happens in a human language. For example, somebody can say, if a condition is true, do that and that and that.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The if Statement
In ActivePerl, 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. Let us look at an example. Consider the following code:

use strict;

print "Content-Type: text/html\n\n";

my $hisVar = 20;

if ($hisVar == 20)
{
print "I am studying ActivePerl, which is like Perl.";
}

I advice you to always start with the statement, "use strict;" If your results are to be sent to the browser as we are doing then you should have the statement 'print "Content-Type: text/html\n\n";' up in your code before any other print statement.

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" statement. The if-statement begins with the reserved word, "if" and ends with the curly brace, }. What goes inside the parentheses is the condition. The statements to be executed are in the curly braces. The if-statement is not an ordinary statement, so it does not end with a semicolon. The whole if-statement (with the parentheses and curly braces) is called an if-construct.

If the condition is correct, ActivePerl will replace it with, true, internally; you do not see it. If it is wrong, ActivePerl 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-statement above can be read like this: if $hisVar equals 20 then display, ' I am studying ActivePerl, which is like Perl.'. Since we assigned the value 20 to $hisVar, the condition of the if-statement 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 cgi-bin 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 about, 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 statement 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;

print "Content-Type: text/html\n\n";

my $hisVar = 36;

if ($hisVar == 20)
{
print 'I am studying ActivePerl';
}
else
{
print 'I am doing something else';
}

In the above 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.

elsif
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. Try it.

use strict;

print "Content-Type: text/html\n\n";

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 coding, 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 coding is used only for situations where only one of the conditions is satisfied (is 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) of something to that effect. 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 coding. The following code illustrates this:

use strict;

print "Content-Type: text/html\n\n";

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 to appreciate this.

Complete Syntax for if-Statement
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 is replaced by the following. Read and try it.

use strict;

print "Content-Type: text/html\n\n";

my $hisVar = 10000;

use Switch;

switch ($hisVar)
{
case (10)
{
print 'Value is small';
}
case (100)
{
print 'Value is medium';
}
case (1000)
{
print 'Value is large';
}
else
{
print '$hisVar is very large';
}
}

The syntax for the switch statement is:

use Switch;

switch ($variable)
{
case (value)
{
statements;
}
case (value)
{
statements;
}

- - -
else
{
statements;
}
}

You begin with the "use Switch;" statement. Then you have the switch statement. 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.

Quotation Marks
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.

Let us stop here and continue in the next part of the series. As you continue, always remember that ActivePerl and Perl are essentially the same things, but they work for different operating systems.

Chrys

Published by Chrys Forcha

I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.