Switch is a syntax in a programming to test mulitple conditions and execute single code blocks
Perl 5.10.1 introduced new experimental feature switch. Use below code to enable switch
use feature "switch";
Syntax:
use feature qw(switch);
my $var = '123';
for ($var) {
when (/^123/) { $result =1 }
when (/^456/) {$result =2 }
when (/^789/) {$result =3 }
default { $result =4}
}
print $result;
With Version 5.14, for can be replaced with given
use feature qw(switch);
my $var = '123';
given ($var) {
when (/^123/) { $result =1 }
when (/^456/) {$result =2 }
when (/^789/) {$result =3 }
default { $result =4}
}
print $result;