Perl if Statement

Summary: in this tutorial, you are going to learn about Perl if statement that allows you to control the execution of code conditionally.

Simple Perl if statement

Perl if statement allows you to control the execution of your code based on conditions. The simplest form of the if statement is as follows:

if(expression);Code language: Perl (perl)

In this form, you can put the if statement after another statement. Let’s take a look at the following example:

my $a = 1;
print("Welcome to Perl if tutorial\n") if($a == 1);Code language: Perl (perl)

The message is only displayed if the expression $a == 1 evaluates to true . How Perl defines true and false? The following rules are applied when Perl evaluates an expression:

  • Both number 0 and string “0” are false.
  • The undefined value is false.
  • The empty list () is false.
  • The empty string "" is false.
  • Everything else is true.

If you do not remember the rules or you doubt whether a condition is true or false, you can always write a simple if statement to test the expression.

The limitation of the Perl if statement is that it allows you to execute a single statement only. If you want to execute multiple statements based on a condition, you use the following form:

if(expression){
   statement;
   statement;
   ...
}Code language: Perl (perl)

Noticed that the curly braces {} are required even if you have a single statement to execute. Watch out C/C++ and C# programmers!

The following flowchart illustrates the Perl if statement:

perl if statement

And the following is an example of using the if statement:

my $a = 1;

if($a == 1){
   print("Welcome to the Perl if tutorial!\n");
   print("another form of the Perl if statement\n");
}Code language: Perl (perl)

Perl if else statement

In some cases, you want to also execute another code block if the expression does not evaluate to true. Perl provides the if else statement that allows you to execute a code block if the expression evaluates to true, otherwise, the code block inside the else branch will execute.

if(expression){
   //if code block;
}else{
   //else code block;
}Code language: Perl (perl)

The following flowchart illustrates the Perl if else statement:

Perl if else

See the following example, the code block in the else branch will execute because $a and $b are not equal.

my $a = 1;
my $b = 2;
if($a == $b){
 print("a and b are equal\n");
}else{
 print("a and b are not equal\n");
}Code language: Perl (perl)

Perl if elsif statement

In some cases, you want to test more than one condition, for example:

  • If $a and $b are equal, then do this.
  • If $a is greater than $b then do that.
  • Otherwise, do something else.

Perl provides the if elsif statement for checking multiple conditions and executing the corresponding code block as follows:

if(expression){
  ...
}elsif(expression2){
  ...
}elsif(expression3{
  ...
}else{
   ...
}Code language: Perl (perl)

The following flowchart illustrates the if elseif else statement:

Perl if elsif else

Technically speaking, you can have many elsif branches in an if elseif statement. However, it is not a good practice to use a lengthy if elsif statement.

my $a = 1;
my $b = 2;

if($a == $b){
   print("a and b are equal\n");
}elsif($a > $b){
   print("a is greater than b\n");
}else{
   print("a is less than b\n");
}Code language: Perl (perl)

Perl if statement example

We are going to apply what we have learned so far to create a simple program called currency converter.

  • We will use a hash to store the exchange rates.
  • To get the inputs from users via the command line, we will use <STDIN>. We use the chomp() function to remove the newline character (\n) from the user’s inputs.
  • We convert the amount from local currency to foreign currency if the currencies are supported.

The following source code illustrates the simple currency converter program:

#!/usr/bin/perl
use warnings;
use strict;

my ($l_curr, $f_curr, $l_amount, $f_amount);

my %rates = (	
     USD => 1,
     YPY => 82.25,
     EUR => 0.78,
     GBP => 0.62,
     CNY => 6.23
);

# print supported currencies
print("Supported currency:\n");
for(keys %rates){
	print(uc($_),"\n");
}

# get inputs from users:
print("Enter local currency:\n");
$l_curr = <STDIN>;

print("Enter foreign currency:\n");
$f_curr = <STDIN>;

print("Enter amount:\n");
$l_amount = <STDIN>;

chomp($l_curr,$f_curr,$l_amount);

# check user's inputs.
if(not exists $rates{$l_curr}){
     print("Local currency is not supported\n");
}
elsif(not exists $rates{$f_curr}){
     print("Foreign currency is not supported\n");
}
else{
     # convert from local currency to foreign currency
     $f_amount = ($rates{$f_curr} / $rates{$l_curr}) * $l_amount;

     # print out the result
     print("$l_amount $l_curr = $f_amount $f_curr","\n");
}Code language: Perl (perl)

In this tutorial, you’ve learned various forms of the Perl if statement including simple if, if else, and if esif else statements to control the execution of the code.

Was this tutorial helpful ?