In the following example we can see the usage of switch case in Perl.
Here we shall be using the 'Switch' CPAN module specifically.
Here the following Program name is 'switch_case.pl'
#!/usr/bin/perl
use Switch;
# Defining an array of numbers
my @arr = (1, 2, 3, 4);
# Selecting any random element from the array
my $randomnumber = $arr[rand @arr];
# using the switch case functionality
switch($randomnumber) {
case 1 { print "First Element Of Array: $randomnumber\n" }
case 2 { print "Second Element Of Array: $randomnumber\n" }
case 3 { print "Third Element Of Array: $randomnumber\n" }
else { print "Fourth Element Of Array: $randomnumber\n" }
}
Steps To Run The Program:
-------------------------
1) On the command prompt goto the directory where the program is created, type the following and hit enter.
perl switch_case.pl
Output of the above program is either of the following lines:
First Element Of Array: 1
or
First Element Of Array: 2
or
First Element Of Array: 3
or
First Element Of Array: 4
