The switch statement in Perl allows a variable to be tested for equality against a list of values. Each value is called a case and the variable being switched on is checked for each switch case. A Switch case implementation is dependent on the Switch module. The article focuses on discussing the switch case in Perl.
Installation
We can't use the switch statement directly in Perl. We have to first install the Switch module and then use it in our code and then can call the switch statement. The installation of the Switch module is different in the case of Linux and Windows.
Linux
Method 1: To install the Switch module in Ubuntu just open a terminal and write the following command. Make sure Perl is already installed.
sudo apt-get install libswitch-perl
Method 2: We can also install it using CPAN. Follow the below steps.
Step 1: Write cpan in your Ubuntu Terminal. A lot of loading and downloading installing and configuration will happen, whenever prompts to write yes just write yes.
Step 2: When prompted What approach do you want? Â (Choose 'local::lib', 'sudo' or 'manual') [local::lib] prompt appears type local::lib and press enter. After successfully installing the prompt to type commands will change to cpan[1]>, then type install Switch.Â
As the switch was already installed I received the above output. Users might get something different if they are installing Switch for the first time.
Windows
There is only a single way to install Switch in windows and that is using CPAN. Follow the below steps:
Step 1: Open CMD or any terminal of the user's choice, VS Code Terminal will also work.
Step 2: Type cpan into that terminal.Â
Step 3: After successful installation, the prompt will change to cpan >
Step 4: Now type installs Switch.

Syntax
Now after the successful installation of Switch, we will import that in our code using the use command.
switch (expression){
case 1 {
To-Do;
}
case 2{
To-Do;
}
.........
else{
some_text;
}
Flowchart of Switch

Implementation
Example 1: Below is the Perl program to implement the Switch module:
Perl
#!/usr/bin/perl
# your code here
use Switch;
$var = 10;
@arr = (10, 20, 30);
switch($var) {
case 10 {
print("$var matches this case\n");
}
case "x" {
print("string type")
}
case [1..10,42] {
print("$var present in list\n");
}
case (\@arr) {
print("$var present in array @arr");
}
else {
print("None of the cases match");
}
}
Output :
Explanation:Â
Here we can see if any of the condition is met our code will not go further even if there is another match. It will just break out of the switch statement. What if we want to get all the matches? To do that we will use the next keyword after certain cases which has a match.
Example 2: Below is the Perl program to implement the next keyword:
Perl
#!/usr/bin/perl
use Switch;
$var = 10;
@arr = (10, 20, 30);
switch($var) {
case 10 {
print("$var matches this case\n");
next; # This will force our code to go and find the next match if any
}
case "x" {
print("string type")
}
case [1..10,42] {
print("$var present in list\n");
next; # This will force our code to go and find the next match if any
}
case (\@arr) {
print("$var present in array @arr");
}
else {
print("None of the cases match");
}
}
Output:
Explanation:Â
Here first of all the first case matches as var holds 10 and the case is also 10 so it prints 10 matches this case, as we have provided next, in this case, it will not exit out of the switch case, rather it will search for another match if any. It finds another match at case [1..10,42], now this case is being considered as a match because here the case is [1,2,3,4,5,6,7,8,9,10,42] and var holds 10, so 10 is present in the list that's why this is also a match. As we again used next it will search for any more matches, and in the next case, it finds the match as arr holds the values (10,20,30) so 10 is present in arr so it is again a match. Now if we use next here and there are some cases after that case and none of them matches, then it will execute the else block.
Example 3: Below is the Perl program to implement the Switch module:
Perl
#!/usr/bin/perl
use Switch;
$var = 10;
@arr = (10, 20, 30);
switch($var) {
case 10 {
print("$var matches this case\n");
next;
}
case "x" {
print("string type")
}
case [1..10,42] {
print("$var present in list\n");
next;
}
case (\@arr) {
print("$var present in array @arr\n");
next;
}
case 48 {
print("This is another case");
}
else {
print("None of the cases match");
}
}
Output:
Similar Reads
C# Switch Statement In C#, Switch statement is a multiway branch statement. It provides an efficient way to transfer the execution to different parts of a code based on the value of the expression. The switch expression is of integer type such as int, char, byte, or short, or of an enumeration type, or of string type.
4 min read
Switch statement in Programming Switch statement in programming enables the execution of different code blocks based on the value of an expression, providing a structured approach to handle multiple cases efficiently. It enhances code readability and simplifies decision-making processes, making it a valuable tool for managing prog
6 min read
Enhancements for Switch Statement in Java 13 Java 12 improved the traditional switch statement and made it more useful. Java 13 further introduced new features. Before going into the details of new features, let's have a look at the drawbacks faced by the traditional Switch statement. Problems in Traditional Switch1. Default fall through due t
5 min read
Switch Statement in C C switch statement is a conditional statement that allows you to execute different code blocks based on the value of a variable or an expression. It is often used in place of if-else ladder when there are multiple conditions.Example:C#include <stdio.h> int main() { // Switch variable int var =
5 min read
Switch Statements in Java The switch statement in Java is a multi-way branch statement. In simple words, the Java switch statement executes one statement from multiple conditions.It is an alternative to an if-else-if ladder statement. It provides an easy way to dispatch execution to different parts of the code based on the v
9 min read