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:

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

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 if the expression.

The limitation of the this 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:

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:

Perl if else statement

In some cases, you want to also execute another code block if the expression does not evaluates 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.

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.

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:

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.

Perl if statement example

We are going to applied 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 command line, we will use <STDIN>. We use the chomp() function to remove newline character (\n) from 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:

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.