Skip Menu |

This queue is for tickets about the Switch CPAN distribution.

Report information
The Basics
Id: 33988
Status: resolved
Priority: 0/
Queue: Switch

People
Owner: Nobody in particular
Requestors: bassbonematt
c0m
gmail
[a_t]
[d_o_t]
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 2.13
Fixed in: (no value)



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." } } }
Subject: Re: [rt.cpan.org #33988] Single prototype in subroutine containing switch block causes syntax error when Switch only incuded outside of subroutine
Date: Wed, 12 Mar 2008 12:57:55 +1100
To: bug-Switch [...] rt.cpan.org
From: Damian Conway <damian [...] conway.org>
Matt Smith via RT wrote: Show quoted text
> 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.
Thanks for the report, Matt. The problem appears to be that Switch uses Text::Balanced::extract_codeblock() to do its compile-time parsing, and that module doesn't really parse Perl, just tries to recognize common constructs. The specific problem is that, when it sees "($)", it treats that as a "(" followed by the special perl variable "$)", which leaves the opening paren unclosed. This is not really fixable without major re-engineering of Switch's internals (i.e. replacing Text::Balanced with a modified version of the PPI module) and I'm disinclined to devote the effort to it, given that Perl 5.10 solves the problem far more effectively by providing an actual given/when statement. Nevertheless, I appreciate the report and especially the suggested workarounds. Damian