http://www.perlmonks.org?node_id=1078449
Deep_Plaid has asked for the wisdom of the Perl Monks concerning the following question:

I have run across a situation where I need a Switch statement. After reading up on this online, it is my understanding that there really is no native Switch statement for PERL, and for later versions you should use the given/when construct (I'm using PERL v5.18.1 for Windows).

The problem is I can't seem to get the syntax right, even when using a dumbed down example I got from the web. Here's the code I'm trying to run:

use strict; use warnings; sub test2 { my $var = 2; my $i; given ($var){ when(1) { $i = "One"; } when(2) { $i = "Two"; } when(3) { $i = "Three"; } default { $i = "Other"; } } print "$var is $i"; } test2();

That gives me the following errors:

syntax error at D:/Eclipse_std_kepler_ws/StudyBuildReport/TestGiven.pl + line 9, near ") {" Global symbol "$i" requires explicit package name at D:/Eclipse_std_ke +pler_ws/StudyBuildReport/TestGiven.pl line 9. Global symbol "$i" requires explicit package name at D:/Eclipse_std_ke +pler_ws/StudyBuildReport/TestGiven.pl line 10. Global symbol "$i" requires explicit package name at D:/Eclipse_std_ke +pler_ws/StudyBuildReport/TestGiven.pl line 11.

If I can't get this to work, I'll be stuck using an if/elsif construct, illustrated below (this example works fine):

use strict; use warnings; sub test1 { my $var1 = 2; my $b; if ($var1 == 1) { $b = "One"; } elsif ($var1 == 2) { $b = "Two"; } elsif ($var1 == 3) { $b = "Three"; } print "$var1 is $b"; } test1();

Any advice you can give would be deeply appreciated. Thanks.