in reply to Help Debug Switch.pm!
Which means that the source isn't getting filtered properly, and so the filtered source ends up with the same case statements that are in your original source--but case isn't a valid Perl bareword, so that's why you get that error.
But that's simply the symptom, really.
The *real* problem is this: Text::Balanced::_match_quotelike is being called, and it matches from the first '?' to the last (the one in the comment), and it sees that as a valid regex. In case this doesn't quite make sense, it sees this:
Question marks ('?') are valid regex delimiters; valid in the sense that you don't need a 'm' in front of them. They're like '/'. You can just say:? 'foo' : 'bar'; switch('x'){ case 'y' {print "y\n"} else {print "x\n"} } # ?
That's legal.if ($foo =~ ?bar?)
Here's a runthrough of what's going on, essentially; it makes the most sense if you follow along in the original source.
It steps through that source, matching against regular expressions, in the form /\Gpattern/gc. This means that each time the regex engine tries to do a match, it starts in the source string *after* the last successful match. So using pos, I tried to figure out what it was doing.my $x = 2 ? 'foo' : 'bar'; switch('x'){ case 'y' {print "y\n"} else {print "x\n"} } # ?
That's three characters: a newline, then 'my'. (I realize it's hard to see the newline. :) This puts pos at 3.my
Which is also three characters: a space, then '$x'. This puts pos at 6.$x
Which is two chars: a space, followed by '='. This puts pos at 8.=
Which is two chars: a space, then '2'. This puts pos at 10.2
We've also been trying to match "things that look like quotes", using Text::Balanced::_match_quotelike. Nothing has matched yet, but the next time we go through, we do:
In _match_quotelike we first match the '$pre' condition, which is optional whitespace. This moves pos to 11. The next thing in the string is '?': _match_quotelike looks at this and enters an if condition, looking to see if this could be a regex match. It looks to see if there's another '?' in the string--and there is! It's hidden in a comment, but _match_quotelike doesn't notice, and so the "quote" matched is:
This puts pos at 124. _match_quotelike returns this match, and this puts us at the end of the string: we're done.? 'foo' : 'bar'; switch('x'){ case 'y' {print "y\n"} else {print "x\n"} } # ?
But it seems to fit pretty well.
UPDATE: I just tested this on Perl 5.6.0, and I got errors there, as well. So it looks like it's matching in 5.6.0 as a '?'-delimited regex as well. The error is slighty different:
But it's certainly the same idea: it doesn't understand what either switch or case is, which means that they didn't get filtered properly. And stepping through using the same debugging statements shows me that it's matching the same things in 5.005_03 as in 5.6.0.syntax error at t.pl line 9, near "){" String found where operator expected at t.pl line 10, near "case 'y'" (Do you need to predeclare case?) Execution of t.pl aborted due to compilation errors.
|
---|