In PHP, you have something like this:
Is there an equivalent in Perl?PHP Code:switch ($variable)
{
case 1:
//do this;
break;
case 2:
//do that;
break;
case 3:
//do something else;
break;
}
Printable View
In PHP, you have something like this:
Is there an equivalent in Perl?PHP Code:switch ($variable)
{
case 1:
//do this;
break;
case 2:
//do that;
break;
case 3:
//do something else;
break;
}
Code:switch ($variable) {
case 1 { do this }
case 2 { do that }
case 3 { do something else }
else { when all else fails }
}
EDIT: Dealt with one error message, still getting a couple others.
Code:#!usr/bin/perl
# switchexperiment.perl
use warnings;
use strict;
my $variable=<STDIN>;
my $word;
chomp $variable;
switch ($variable){
case=1 {
$word="One";
}
case=2 {
$word="Two";
}
case=3 {
$word="Three";
}
else {
$word="Zip";
}
}
print $word;
Quote:
Originally Posted by Error
Whoops. Somehow a line came out missing when I pasted the code into the text box. It should have been this:Code:use Switch;
switch ($variable) {
case 1 { do this }
case 2 { do that }
case 3 { do something else }
else { when all else fails }
}
So now what do I do?Quote:
Can't locate Switch.pm in @INC (@INC contains: C:/Perl/lib C:/Perl/site/lib .) at C:\Program Files\xampp\htdocs\Pages\Scripts\switchexperiment.perl line 6.
BEGIN failed--compilation aborted at C:\Program Files\xampp\htdocs\Pages\Scripts\switchexperiment.perl line 6.
I would actually suggest that you update your version of Perl. The Switch module is one of the core modules that comes prepackaged with Perl, but it is possible that you have an older release of Perl that did not include it.
If you do not want to install the latest version of Perl, then you will need to install this module. If you installed ActiveState's ActivePerl, then you can use their Perl Package Manager; if you compiled Perl on your machine, then you should use the CPAN module.
Is Perl 5.6 old, then?
The 5.6.x versions began in 2000 and ended in 2003, so yes. ;)
The newest release at the moment seems to be 5.8.8.
Where can I get the latest?
Here we go! Thanks!
F.Y.I. The switch module is now deprecated. There is now the given-when statement which is much more powerful. Or you can simply is the if-else control structure.
Holy thread necromancy, Batman! 6+ years old and resolved to boot!