The switch is a syntax in programming to test multiple conditions and execute single code blocks

Perl 5.10.1 introduced a new experimental feature switch. Use the below code to enable the 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 the 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;