Open In App

Perl | given-when Statement

Last Updated : 04 Jul, 2019
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

given-when statement in Perl is a substitute for long if-statements that compare a variable to several integral values.

  • The given-when statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
  • given is a control statement that allows a value to change control of execution.

given-when in Perl is similar to the switch-case of C/C++, Python or PHP. Just like the switch statement, it also substitutes multiple if-statements with different cases.

Syntax:

given(expression)
{
    when(value1) {Statement;}
    when(value2) {Statement;}

    default {# Code if no other case matches}
}

given-when statements also use two other keywords along with it, namely break and continue. These keywords maintain the flow of the program and help in getting out of the program execution or skipping execution at a particular value.

break: break keyword is used to break out of a when block. In Perl, there is no need to explicitly write the break after every when block. It is already defined implicitly.
continue: continue, on the other hand, moves to the next when block if first when block is correct.

In a given-when statement, a conditional statement must not repeat in multiple when statements, this is because Perl checks for the first occurrence of that condition only and the next repeating statements will be ignored. Also, a default statement must be placed after all the when statements because the compiler checks for condition matching with every when statement in order, and if we will place default in between, then it will take a break over there and will print the default statement.

Example:




#!/usr/bin/perl
  
# Perl program to print respective day
# for the day-code using given-when statement
use 5.010;
  
# Asking the user to provide day-code
print "Enter a day-code between 0-6\n";  
  
# Removing newline using chomp
chomp(my $day_code = <>);  
  
# Using given-when statement
given ($day_code
{  
    when ('0') { print 'Sunday' ;}  
    when ('1') { print 'Monday' ;}  
    when ('2') { print 'Tuesday' ;}  
    when ('3') { print 'Wednesday' ;} 
    when ('4') { print 'Thursday' ;}  
    when ('5') { print 'Friday' ;} 
    when ('6') { print 'Saturday' ;}
    default { print 'Invalid day-code';}  
}  


Input:

4

Output:

Enter a day-code between 0-6
Thursday

Nested given-when statement

Nested given-when statement refers to given-when statements inside of another given-when Statements. This can be used to maintain a hierarchy of inputs provided by the user for a specific output set.

Syntax:

given(expression)
{
    when(value1) {Statement;}
    when(value2) {given(expression)
                  {
                     when(value3) {Statement;}   
                     when(value4) {Statement;}   
                     when(value5) {Statement;} 
                     default{# Code if no other case matches}
                  }
                 }
    when(value6) {Statement;}

    default {# Code if no other case matches}
}

Following is an example for Nested given-when statement:




#!/usr/bin/perl
  
# Perl program to print respective day
# for the day-code using given-when statement
use 5.010;
  
# Asking the user to provide day-code
print "Enter a day-code between 0-6\n";  
  
# Removing newline using chomp
chomp(my $day_code = <>);  
  
# Using given-when statement
given ($day_code
{  
    when ('0') { print 'Sunday' ;}  
    when ('1') { print "What time of day is it?\n";
                 chomp(my $day_time = <>);
                   
                 # Nested given-when statement
                 given($day_time)
                 {
                     when('Morning') {print 'It is Monday Morning'};
                     when('Noon')    {print 'It is Monday noon'};
                     when('Evening') {print 'It is Monday Evening'};
                     default{print'Invalid Input'};
                 }
                }  
    when ('2') { print 'Tuesday' ;}  
    when ('3') { print 'Wednesday' ;} 
    when ('4') { print 'Thursday' ;}  
    when ('5') { print 'Friday' ;} 
    when ('6') { print 'Saturday' ;}
    default { print 'Invalid day-code';}  
}  


Input:

1
Morning

Output:

Enter a day-code between 0-6
What time of day is it?
It is Monday Morning

Input:

3

Output:

Enter a day-code between 0-6
Wednesday

In the above-given Example, when the Input day-code is anything but 1 then the code will not go into the nested given-when block and the output will be same as in the previous example, but if we give 1 as Input then it will execute the Nested given-when block and the output will vary from the previous example.



Previous Article
Next Article

Similar Reads

Perl | Basic Syntax of a Perl Program
Perl is a general purpose, high level interpreted and dynamic programming language. Perl was originally developed for the text processing like extracting the required information from a specified text file and for converting the text file into a different form. Perl supports both the procedural and Object-Oriented programming. Perl is a lot similar
10 min read
Perl | goto statement
The goto statement in Perl is a jump statement which is sometimes also referred to as unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function. Syntax: LABEL: Statement 1; Statement 2; . . . . . Statement n; goto LABEL; In the above syntax, the goto statement will instruct the compiler to imme
3 min read
Perl | chomp() Function
The chomp() function in Perl is used to remove the last trailing newline from the input string. Syntax: chomp(String) Parameters: String : Input String whose trailing newline is to be removed Returns: the total number of trailing newlines removed from all its arguments Example 1: C/C++ Code #!/usr/bin/perl # Initialising a string $string = &amp;quo
2 min read
Perl | Backtracking in Regular Expression
In Perl, a Regular expression(a.k.a regexes or regexps or REs) is a way of describing a set of strings without having to list all strings in your program or simply we can say that it is a sequence of characters that are used for pattern matching. In Perl, regular expressions have different uses: Firstly, they are used in conditionals to determine w
3 min read
Perl | Searching in a File using regex
Prerequisite: Perl | Regular Expressions Regular Expression (Regex or Regexp or RE) in Perl is a special text string for describing a search pattern within a given text. Regex in Perl is linked to host language and are not the same as in PHP, Python, etc. Sometimes these are termed as "Perl 5 Compatible Regular Expressions". To use the Regex, Bindi
6 min read
Perl | Operators | Set - 1
Operators are the main building block of any programming language. Operators allow the programmer to perform different kinds of operations on operands. In Perl, operators symbols will be different for different kind of operands(like scalars and string). Operators Can be categorized based upon their different functionality: Arithmetic OperatorsRelat
12 min read
Perl | lt operator
'lt' operator in Perl is one of the string comparison operators used to check for the equality of the two strings. It is used to check if the string to its left is stringwise less than the string to its right. Syntax: String1 lt String2 Returns: 1 if left argument is less than the right argument Example 1: #!/usr/local/bin/perl # Initializing Strin
1 min read
Perl | chop() Function
The chop() function in Perl is used to remove the last character from the input string. Syntax: chop(String) Parameters: String : It is the input string whose last characters are removed. Returns: the last removed character. Example 1: #!/usr/bin/perl # Initialising a string $string = &quot;GfG is a computer science portal&quot;; # Calling the chop
1 min read
Perl | rename() Function
rename() function in Perl renames the old name of a file to a new name as given by the user. Syntax: rename(old_file_path, new_file_path) Parameters: old_file_path: path of the old file along with its name new_file_path: path of the new file along with its name Returns 0 on failure and 1 on success Example: #!/usr/bin/perl -w # Calling the rename()
1 min read
Perl | Subroutines or Functions
A Perl function or subroutine is a group of statements that together perform a specific task. In every programming language user want to reuse the code. So the user puts the section of code in function or subroutine so that there will be no need to write code again and again. In Perl, the terms function, subroutine, and method are the same but in s
2 min read
Article Tags :
three90RightbarBannerImg