Perl IF Statement- A Versatile Conditional Statement Construct

perl operatorsPERL is an acronym for Practical extraction and report language. Many of Perl’s features are taken from other languages such as C and BASIC. This language supports both procedural and object oriented programming. Developers coming from C or Unix shell programming background will find it relatively easy to migrate to this platform. Perl programs are known to be stable. Applications created with Perl will run on a wide variety of platforms.  Perl possesses superior text manipulation capabilities. This language is open source and extensible.  Perl offers convenient connectivity to databases such as Oracle, Sybase, MySQL and others. The language is capable to handle e-commerce transactions. It is often referred to as ” the duct-tape of the Internet”. Perl is Y2K compliant and offers support for Unicode.  This language simplifies the handling of HTML forms. Perl is often embedded in web servers in order to significantly increase processing speeds.

We walk you through this beginner’ s level tutorial on the Perl if statement. We assume that you are familiar with the basics of programming. Else we suggest you go through this course on programming for complete beginners.

The Perl If statement

The if statements tests whether a condition or regular expression evaluates to true. If yes, then a block of statement associated with the if is executed. Else, the execution transfers to the next block. Let’s take a look at the different kinds of IF statements in Perl.

1.     A simple if statement

The syntax of this statement is as follows:

if (EXPR) BLOCK

Here EXPR is evaluated. If true, Perl will execute the code between the curly braces of the block. If false, program control will go to the next statement after the block. Take a look at the example given below

$x = -1;
if($x < 0) {
$x += 2;
}
print "x = $x\n";

The is a very simple program that just checks if X is less than 0. If yes, we add 2 to X and print it. Learn how to write your own Perl programs with this course.

if(1) {
$a = 1;
}

In the above example, the if statement will always be true. Note that no condition is used here. We just check the value of ‘1’ – which is always true. Learn more about evaluating expressions in this course. Note that the principles are common across programming languages.

2.     General if statement

if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK

The parameters are as follows

  • EXPR stands for a condition.
  • BLOCK consists of statement(s) enclosed in curly braces.
  • else and elsif clauses are optional.

3.     if-else

Here an if statement may be  followed by an else statement.

The syntax is as follows

if (EXPR) BLOCK else BLOCK

Example 1: Program to find the maximum of two numbers

$x = 15;
$y = 27;
if($x > $y) {
print "max($x, $y) = $x\n";
} else {
print "max($x, $y) = $y\n";
}

Here the variable x and y are assigned the values 15 and 27 respectively. The if statement evaluates whether value of variable x is greater than value of variable y. Since the condition proves to be false, the statements enclosed within the curly braces of the if statement are skipped. The program control goes to the else statement. Else does not have any condition. Hence the statement enclosed in it is executed.

The if statement comes in the category of conditional statements. In order to advance your knowledge on conditional statements in Perl, check out this hands-on Perl course. 

4.     if used as a modifier

Here the syntax  is as follows.

STATEMENT if (EXPR)

Here we use Perl if as a modifier of a statement. In order to execute the if statement, EXPR (which is a condition) must be true. This format of if allows you to put the important statement right at the beginning. The example given below demonstrates that.

$debug = 1;
$x = 1; $y = 2;
print "x = $x\n" if $debug;
print "y = $y\n" if $debug;

In the above code, if the debug message has to be printed at the beginning we set its value to 1.

Example 2: Program using if statement along with logical operators

#!/usr/bin/perl
print "content-type: text/html \n\n";
$a = 7;
$b = 7;
if ($a == 7) {
print '$a is equal to 7!';
print "<br />";
}
if (($a == 7) || ($b == 7)) {
print '$a or $b is equal to 7!';
print "<br />";
}
if (($a == 7) && ($b == 7)) {
print '$a and $b are equal to 7!';
print "<br />";
}

Initially, the variables a and b are assigned the value 7. In the first if statement, we use the equality operator’==’. Note that this is different from the assignment operator ‘=’. As the first if condition evaluates to true, the statements enclosed in its braces are executed. Then we evaluate the second if condition. Note that the logical operator for or “||” is used in the condition. In the event, that any one or both the conditions are true, the if statement evaluates to true. IN the third if condition we use the logical and operator “&&”. This operator returns true only if both the operands are true.

Example 3:  If else statement using relational operators

$name1 = "Jimmy";
$num1 = 7;
if ($num1 > 8) {
print "$num1 is greater than 8!";
} else {
print "$num1 is not greater than 8!";
}
print "<br />";
if ($name1 eq "Jimmy") {
print "Hello, $name1!";
} else {
print "You are not $name1!";
}

Here there are two if else statements. In the first if statement the relational operator ‘>’ is used. If it evaluates to true, the statement within the if statement’s curly braces are executed. In case, if the condition evaluates to false the statements falling under the else statement’s curly braces are executed. In the second if statement, the relational operator ‘eq’ is used. It stands for equality. Depending upon whether the if condition is true or false, the respective statements in the curly braces are executed.

we hope this gives you a good insight into the various ways you can use the IF statement in Perl. Do experiment with the examples given here, and try out your own code. As always, programming makes perfect.