This program throws an error. The problem is that I have to use switch
case
. How can I do this in Perl?
#use strict;
#use warnings;
my $input = print "Enter the number";
$input = <STDIN>;
switch($input){
case "1" {print "UPC"}
case "2" {print "ES"}
case "3" {print "MS"}
else {print "Enter the correct value"}
}
You need to import Switch to use it:
use Switch;
However, Switch has been deprecated. See this question: Why is the Switch module deprecated in Perl?
Some alternatives (and their experimental status) are discussed here: http://perldoc.perl.org/perlsyn.html#Switch-Statements
In summary, if you're using Perl >5.10.1, you can use the following for a non-deprecated, non-experimental switch:
use v5.10.1;
for ($var) {
when (/^abc/) { $abc = 1 }
when (/^def/) { $def = 1 }
when (/^xyz/) { $xyz = 1 }
default { $nothing = 1 }
}