Perl Switch: Is There a Switch Statement in Perl?

perl switch keyPerl is the language of choice when it comes to text processing. You can use Perl to extract data from text files and print out reports. You can also use Perl to convert the text file into another form (like html). However, that is not all that Perl can do. Perl is a high- level programming language. Like many of the other modern programming languages, Perl is an object oriented language. You can use Perl to write applications or run web servers. It can act as an interface language for databases, or it can be used to run games. Perl is an open source language, and most of its development is carried out by a community of Perl programmers. Unlike Java, Perl won’t cost you a dime if you use it for your large-scale projects.

If you’re new to programming, you won’t find it difficult to pick up Perl. The syntax, or the coding, of the language is simple and easy to remember. It also has several features, like automatic garbage collection and memory management, that make writing programs (or scripts as they’re called in Perl) an enjoyable experience. Work as a Perl programmer is easy to find – many top organizations and governments the world over use Perl in some form or the other. If you want to learn Perl, take this introductory Perl programming course. You can learn the basics as well as the advanced concepts. You won’t have to waste precious hours searching the internet for info- you’ll get everything at one place.

The Perl Switch Statement: Does it Exist?

Perl does not have an official switch statement at the moment. It supplied a “given” statement in version 5.10, which replicated the functions of the switch statement that you find in other languages like C. The given statement was similar to the switch statement, except much more powerful. However, the “given” statement was downgraded as experimental in Perl 5.18 and programmers were advised to avoid using it in their scripts. As of now, it is unclear what will happen to the given statement in the upcoming Perl 5.20 release.

In this tutorial, we’ll give you a brief overview of what a switch conditional statement is and what it’s supposed to do. We’ll also give you the syntax for the given statement, though we recommend that you avoid using it in your coding until its cleared in a Perl release. You can use if… else…if instead.

The Switch Statement in Perl

So what is the switch statement exactly? It is a conditional statement that tells Perl to perform a certain task if a condition is true. How is it different from the other conditional statements? The only difference is that the switch statement gives you more than a couple of options to choose from.

Consider the following bit of code:

if(a == 1) {
statement 1;
}
elsif(a == 2) {
statement 2";
}
elsif(a == 3) {
statement 3;
}
elsif(a == 4) {
statement 4;
}
else {
statement 5;
}

Here, we’re using the else…if conditional statement to get Perl to execute one of 5 possible statements. If the value of variable a is 1, statement 1 will be executed. If the value of variable a is 2, statement 2 will be executed, and so on. If the value of variable a is not 1, 2, 3 or 4, the fifth statement (or the default statement) will be executed.

The switch statement does exactly what we have done in the script above. The only difference is that it is more efficient at it and makes your code more readable. See the example below:

switch(a) {
case 1:
statement 1;
break;
case 2:
statement 2;
break;
case 3:
statement 3;
break;
case 4:
statement 4;
break;
default:
statement 5;
}

Please note that this is just an example of what the switch statement is supposed to do, and this is not how it works in Perl. In the code above, switch statement will ask Perl execute one of the 5 possible statements. If case number 3 was true, for example, it will execute statement three and break (or exit) the script.

You can use the else…if statement to do what the switch statement can. To learn more about it, and other conditional statements, sign up for our this C programming course, which explains the concepts effectively.

Perl Given ()

Like we mentioned before, there is no official switch statement in Perl at present, though you can get around this limitation by using the else…if statement (and there are other ways too). The given statement, though experimental, can be used. Please note that using the given statement will throw up errors occasionally (cause it is experimental).

The syntax for the given statement is as follows:

given($variable )
{
when (1) { execute 1; }
when (2) { execute 2 ; }
when (99) {execute 99; }
default {execute ; }
}

The syntax is simple enough to understand. The $variable will hold the value that will be checked by the given statement. If the value is 1, statement 1 will be executed. If the value is 99, statement 99 will be executed. The when keyword is similar to the case keyword – it checks the value of the variable we specified.

Let’s take a simple example script:

given($a )
{
when (1) { say “One”; }
when (2) { say “Two”; }
when (99) { say “Three”; }
default { say “The variable you entered is not present in our database.” }
}

Output:

One
or
Two
or
Three
or
The variable you entered is not present in our database.

Here, we are using a variable $a to hold the value that will be checked by the given statement. Depending on its value, the corresponding statement will be executed. The given statement can check for any kind of values, not just integers. This Perl course can help you learn how to write your own scripts in Perl. Try writing a script that accepts a string variable instead of an integer. You can also check out how switch cases are used in other scripting languages, with this course that introduces Bash scripting and Python.