Updated April 3, 2023
Introduction to Perl Switch statement
Switch statement is used to evaluate a value through multiple conditions, if one condition does not match, will go for another, if second condition does not match will go the another one, etc. Perl language does not support switch statement, to implement the switch in perl given and when statement is used where given is used in case of switch and when is used in case of case. switch and case has been used earlier in Perl, but it gives some compilation errors. To solve these errors, later on Perl language just upgraded it and implement given and when to use for switch statement.
Syntax:
given(expression)
{
when ( first value)
{
statement to be executed;
}
when (second value)
{
statement to be executed;
}
....
...
when (nth value)
{
statement to be executed;
}
default
{
statement to be executed if all the cases are not matched.;
}
}
How does Switch Statement works in Perl?
Given and when keywords are used to implement switch in Perl. given keyword is used in place of switch that handles the expression or value that needs to be evaluated. when is used in place of case that handles various conditions and based on the user input, it executes the appropriate case. That is first it checks case 1 i.e. first condition, if it matched with user input, it will execute that loop, otherwise it will transfer flow control to another case i.e. another condition. If the second condition will be matched with the user input, it will execute that case else transfer to the third case. Likewise, it will execute case until the match founds. Once the match founds with user input, it will terminate the program. If none of the mentioned conditions matched with the user input, it will execute the default block. The default block is used to execute the statements if the user input does not match with conditions.
Examples to Implement Switch Statement in Perl
Here are some examples mentioned below:
Example #1
To print the Months in Perl
Code:
use feature qw(switch say);
print "Enter the Number for the Month \n";
chomp( my $month = <> );
given ($month)
{
when('1')
{
say "January";
}
when('2')
{
say "February";
}
when('3')
{
say "March";
}
when('4')
{
say "April";
}
when('5')
{
say "May";
}
when('6')
{
say "June";
}
when('7')
{
say "July";
}
when('8')
{
say "August";
}
when('9')
{
say "September";
}
when('10')
{
say "October";
}
when('11')
{
say "November";
}
when('12')
{
say "December";
}
default
{
say "Please Enter valid Month Number";
}
}
Output:
When user will enter 10, it will show the result as shown in the below figure
When user will enter the values which is not mentioned in the case, it will execute the default statement and print the result as shown in the below figure.
Explanation: Here we have written a program to print the specific month based on the user input. use feature qw(switch say); library is used to implement switch in Perl using given and when statement. The first program will prompt the message “Enter the Number for the Month” for the user. A variable month is used to store the value enter by the user. Then given keyword will switch the value of the month and transfer flow control to the switch conditions. Based on the user input, the appropriate condition will execute.
Example #2
To print the week days in Perl
Code:
use feature qw(switch say);
print "Enter the Number for the Week \n";
chomp( my $week = <> );
given ($week)
{
when('1')
{
say "Monday";
}
when('2')
{
say "Tuesday";
}
when('3')
{
say "Wednesday";
}
when('4')
{
say "Thursday";
}
when('5')
{
say "Friday";
}
when('6')
{
say "Saturday";
}
when('7')
{
say "Sunday";
}
default
{
say "Please Enter valid Week Number";
}
}
Output:
When the user enters 5, it will show the result as shown in the below figure.
When user enters the values which is not mentioned in the case, it will execute the default statement and print the result as shown in the below figure.
Explanation: Here we have written a program to print the weekdays based on the user input. The example is the same as the first example. First program prompt message “Enter the Number for the Week”. week variable is used to store the user input. The execution is the same as the first example
Conclusion
In this article we have seen how when and given statements are used in Perl instead of Switch and case statement along with the syntax and programs. I hope you will find this article helpful.
Recommended Articles
We hope that this EDUCBA information on “Perl Switch” was beneficial to you. You can view EDUCBA’s recommended articles for more information.