At 11:04 PM 8/18/00 -0600, Tony Olekshy wrote:
>As currently promulgated, catch "Foo" {} will always catch,
>because "Foo" is true. Will this cause confusion for developers
>who meant to say catch Foo {}?
This is a good point, but I'm not about to concede an extra keyword
:-) Let's look at some possibilities:
1. Users already know they can't use strings for class names in, e.g.,
my $foo = new Bar::Baz;
- it gives an error. So the chance of it happening is small.
2. We could apply dwimmery in the case where it does. If a catch
expression evaluates to the name of a known class, output a warning and
keep going.
3. The case you point out - constant string used instead of classname - is
by far the most likely occurrence of the mistake. So just have the parser
look for it and issue a compilation warning.
> And what happens when someone
>says catch "Foo", "Bar" {}?
Ah, that's easy: syntax error. See below.
>We can't just say that catch Foo {} and catch "Foo" {} are the
>same thing, or that catch "Foo" {} is outlawed, because catch
>$test {} is supposed to work even if $test *is* a string.
>
>Or can we? I'm not a parser expert.
>And while I'm on the topic, how likely is it that
>
> catch <expr> { ... }
>
>is going to be parsable? Does Perl 5 do that in any cases?
Well, look at map. Perl can parse
map <expr>, <list>
We're asking it to find a left brace instead of a comma. Where might this
break down? Here's one possibility:
my ($hash,%hash);
...
catch $hash { ... }
Okay, suppose we change the syntax to
catch <expr>, <block>
We know that's doable because map can do it. Suppose we just require that
comma in all cases except the catch-all. Then we can write:
catch Exception::IO => { ... }
catch Exception::IO, Exception::SQL => { ... }
(This one should be doable because the parser can tell that it hasn't found
a block after that first comma, so it knows to keep going)
catch $@->isa('Exception::Arithmetic') => { ... }
catch { ... }
Now the question is whether perl can tell that block isn't some expression
like an anonymous hash constructor. I'm no expert - my guess is that it
could tell by the time it discovered no comma following the block.
--
Peter Scott
Pacific Systems Design Technologies