Perl given (original) (raw)

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 allow 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:

use v5.10;Code language: Perl (perl)

Or use the following pragma:

use feature "switch";Code language: Perl (perl)

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:

given(expr){ when(expr1){ statement;} when(expr1){ statement;} when(expr1){ statement;} … }Code language: Perl (perl)

Let’s examine the given statement in greater detail:

Versions:

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 the user to input an RGB (red, green, blue) color and returns its color code:

`use v5.10; # at least for Perl 5.10 #use feature "switch"; use warnings; use strict;

my $color; my $code;

print("Please enter a RGB color to get its code:\n");

$color = ;

chomp($color); color=uc(color = uc(color=uc(color);

given($color){

 when ('RED') {  $code = '#FF0000'; }

 when ('GREEN') {  $code = '#00FF00'; }

 when ('BLUE') {  $code = '#0000FF'; }

 default{
     $code = '';
 }

} if($code ne ''){ print("code of coloriscolor is coloriscode \n"); } else{ print("$color is not RGB color\n"); }`Code language: Perl (perl)

How program works

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

given($color){ $code = '#FF0000' when 'RED'; $code = '#00FF00' when 'GREEN'; $code = '#0000FF' when 'BLUE'; default{ $code = '';} }Code language: Perl (perl)

As you see, the code is more elegant.

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

print do{ given($color){ "#FF0000\n" when 'RED'; "#00FF00\n" when 'GREEN'; "#0000FF\n" when 'BLUE'; default { '';} } }Code language: Perl (perl)

The code is even more elegant!

Let’s get into a more complex example:

`use v5.12; use strict; use warnings;

print 'Enter something: '; chomp( my $input = <> );

print do { given ($input) { "The input has numbers\n" when /\d/; "The input has letters\n" when /[a-zA-Z]/; default { "The input has neither number nor letter\n"; } } }`Code language: Perl (perl)

How the program works.

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.

Was this tutorial helpful ?