G
Gabkin
I want to use a switch .. case statement in my program.
I looked it up with 'perldoc switch' and found the help.
I tested some code from the perldoc switch help and it failed.
This is what I tried...
<PERL CODE>
use switch;
print("Enter a digit\n");
$var = <STDIN>; chop $var;
print("That digit was ".classify_digit($var)."\n");
exit;
sub classify_digit {
switch ($_[0]) {
case 0 { return 'zero' }
case [2,4,6,8] { return 'even' }
case [1,3,4,7,9] { return 'odd' }
case /[A-F]/i { return 'hex' }
case else { return 'other' }
}
}
</PERL CODE>
And this is the error message I got...
<TESTING...>
bash$ perl misc/test_switch.pm
Number found where operator expected at misc/test_switch.pm line 8, near
"case 0"
(Do you need to predeclare case?)
syntax error at misc/test_switch.pm line 7, near ") {"
syntax error at misc/test_switch.pm line 9, near "case ["
Execution of misc/test_switch.pm aborted due to compilation errors.
</TESTING...>
Using perl, v5.8.2 on Cygwin.
The code seems fine to me.
Why is this switch statement failing?
I have looked at the help and its not helping, I am copying the sample
code and its also failing.
What am I doing wrong here?
I looked it up with 'perldoc switch' and found the help.
I tested some code from the perldoc switch help and it failed.
This is what I tried...
<PERL CODE>
use switch;
print("Enter a digit\n");
$var = <STDIN>; chop $var;
print("That digit was ".classify_digit($var)."\n");
exit;
sub classify_digit {
switch ($_[0]) {
case 0 { return 'zero' }
case [2,4,6,8] { return 'even' }
case [1,3,4,7,9] { return 'odd' }
case /[A-F]/i { return 'hex' }
case else { return 'other' }
}
}
</PERL CODE>
And this is the error message I got...
<TESTING...>
bash$ perl misc/test_switch.pm
Number found where operator expected at misc/test_switch.pm line 8, near
"case 0"
(Do you need to predeclare case?)
syntax error at misc/test_switch.pm line 7, near ") {"
syntax error at misc/test_switch.pm line 9, near "case ["
Execution of misc/test_switch.pm aborted due to compilation errors.
</TESTING...>
Using perl, v5.8.2 on Cygwin.
The code seems fine to me.
Why is this switch statement failing?
I have looked at the help and its not helping, I am copying the sample
code and its also failing.
What am I doing wrong here?