Subject: | Single prototype in subroutine containing switch block causes syntax error when Switch only incuded outside of subroutine |
When declaring and defining a subroutine that contains a switch block,
if you include a prototype for a single parameter, this will cause a
syntax error on compile if you've included Switch outside of the
subroutine. Prototyping multiple parameters does not demonstrate this
bug. I've tested this on Windows XP using ActivePerl (Perl v5.10.0), and
I've tested it on Mac OS X 10.5 (Perl v5.8.8). A friend running Linux
also tested this, and he was able to reproduce the bug.
Here's some example code that will cause the erroneous syntax error on
compile:
#!/bin/perl
use Switch;
my $foo = mysub(1);
print "$foo\n";
exit;
sub mysub($)
{
my $parm = shift;
switch ($parm) {
case (1) { return "You said 1." }
else { return "You didn't say 1." }
}
}
#END CODE EXAMPLE
There are two work-arounds.
1) Use a superfluous semicolon after the single parameter in the
prototype, e.g.: sub mysub($;)
2) Reiterate the 'use Switch;' just before the switch block, e.g.:
sub mysub($)
{
my $parm = shift;
use Switch;
switch ($parm) {
case (1) { return "You said 1." }
else { return "You didn't say 1." }
}
}