Open In App

Perl Switch Case

Last Updated : 05 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Switch module Linux installation

 

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. 

Switch installation using cpan

 

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.

Switch module installation Windows

 

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

Flowchart

 

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 :

Output Switch module

 

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:

Output next keyword

 

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:

Output Switch Module

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads