Syntax:
The syntax of the “given-when” statement which is similar to the “switch-case” statement is given in the following. The value that is assigned in the variable in the given statement is checked with each condition of the “when” statement. If any match is found, the script of the corresponding “when” block is executed.
when (condition) { scripts; }
when (condition) { scripts; }
when (condition) { scripts; }
. . .
default { scripts; }
}
Implementing the “Switch” Statement in Perl
Different uses of the “switch” statement using the “given-when” block are shown in this part of the tutorial.
Example 1: Using “Switch” for a Single Value
In this example, the ID value is taken from the user and checked with the value that is defined in each “when” block. If the input value is matched with the value of any “when” statement, the corresponding message is printed. If no matching ID is found, the message of the default value is printed.
use strict;
use warnings;
use 5.34.0;
#Add this line to remove the warning message
use experimental qw(switch);
#Take the ID value from the user
print "Enter your id: ";
chomp( my $id = <> );
#Print message based on the matching id value
given ($id) {
when ('18956') { say 'You are in group-3.' ;}
when ('19999') { say 'You are in group-2.' ;}
when ('17457') { say 'You are in group-1. ' ;}
default { say 'You are not selected.';}
}
Output:
The following output is printed for the input value of 19999:
The following output is printed for the input value of 78453:
Example 2: Using “Switch” for Multiple Values
In this example, a mark value is taken from the user and it is checked with the range of numeric data. If the input value is matched with any range value, the corresponding grade value is printed. If no matching value is found, the message of the default part is printed.
use strict;
use warnings;
use 5.34.0;
#Add this line to remove the warning message
use experimental qw(switch);
#Take the mark value from the user
print "Enter your mark:";
chomp(my $mark = <> );
#Print grade based on the mark value
given ($mark)
{
when([80..100])
{
say "You got A+";
}
when([75..79])
{
say "You got A";
}
when([70..74])
{
say "You got A-";
}
when([65..69])
{
say "You got B+";
}
default
{
say "You have failed.";
}
}
Output:
The following output is printed if 84 is taken as the mark value:
The following output is printed if 60 is taken as the mark value:
Example 3: Using the “Switch” Statement with Regex
In this example, each value of the array is checked with each regular expression pattern that is defined in each “when” statement. If the value is matched with any pattern, the message of the corresponding “when” block is printed. If the value does not match with any pattern, the message of the default block is printed.
use strict;
use warnings;
use 5.34.0;
#Add this line to remove the warning message
use experimental qw(switch);
#Declare an array of mixed data
my @names = ( 'Md. Minhaz', 786, '####', 'Kamal hossain', ' ', 'Joya Hasan');
#Read the array values
foreach my $v (@names) {
chomp($v);
given ($v)
{
#Match with alphabetic characters
when (/[a-z.]/i) { say "$v is a valid name." ;}
#Match with numeric characters
when (/[0-9]/) { say "Name can't contain number. $v is invalid." ;}
#Match with special characters
when (/[#\t\n]/) { say "Name can't contain non-alphabetic characters. $v is invalid.";}
#Print the default message
default { say "Name can't be empty.";}
}
}
The following output is printed after executing the script. According to the output, the first, fourth, and sixth values of the array are valid. The second value is invalid because the numeric data is used. The third value is invalid because a special character is used. The fifth value is invalid because the value is empty:
Conclusion
Multiple uses of the “given-when” block which is similar to the “switch-case” block are shown in this tutorial using the Perl script.