Perl given

Summary: in this tutorial, you will learn about Perl given statement, which is similar to the switch case statement in other languages.

The given statement works like a series of if statements that allows you to match an expression or variable against different values, depending on the matched value, Perl will execute statements in the corresponding when clause.

The Perl given statement is similar to the switch case statement in other languages such as C/C++, Python or PHP.

Pragma for using given statement

Perl introduced the given statement since version 5.10. In order to use the Perl given statement, you must use the following pragma:

Or use the following pragma:

Perl given syntax

There are several new keywords introduced along with the given such as: when, break and continue.

The syntax of the given statement is as follows:

Let’s examine the given statement in greater detail:

  • Both given and when accept arguments in scalar context.
  • The type of argument you pass to the given clause determines the kind of pattern matching that Perl will use to perform matching. If the argument appears to be a Boolean expression, Perl evaluates it directly. Otherwise, Perl will use smartmatch operator to evaluate the argument, something like $_ ~~ expr
  • To break out a when block, you use the break statement. Perl uses break statement implicitly for all when blocks so you don’t have to explicitly specify it.
  • To fall through from one case to the next, you use the continue statement.

Versions:

  • From version 5.12 you can use when as a statement modifier.
  • From version 5.14, the given statement returns the last evaluated expression if no condition is true or the last evaluated expression of the default clause. The given statement also returns an empty list when the break statement is encountered or no condition is matched.

The following flowchart illustrates the Perl given statement:

Perl given

Sound simple isn’t it? Let’s take a look at some examples of using the given statement to get a better understanding.

Perl given statement examples

The following program asks user to input a RGB (red, green, blue) color and returns its color code:

How program works

  • First, we declared the pragma use v5.10; in order to use the given statement.
  • Second, we asked user for a color, we removed the newline by using the chomp() function and made the input color upper case so that whatever format of color user entered is accepted e.g., Red, rEd or RED is red color.
  • Third, we used the given statement to check. If no color found, then we set the color code to blank in the default clause. Based on user’s input, we got the corresponding color code and display it.

From Perl version 5.12, you can use the when statement as a statement modifier like the following example:

As you see, the code is more elegant.

In addition, the given statement returns a value that is the result of the last expression.

The code is even more elegant!

Let’s get into a more complex example:

How program works.

  • The program asks user to enter anything that can be numbers, letters, or both.
  • In the when clause, we used a very special expression that is known as a regular expression. The /\d/ matches any string contains only numbers, The /[a-zA-Z]/ expression matches string that contains only letters. In this case, the given statement can do more advanced matches.

In this tutorial, we’ve shown you how to use the Perl given statement that allows you to test a condition against different values and execute code blocks conditionally.