Rui Maciel <> writes:
> Ben Bacarisse wrote:
>
>> Perl has a wonderful construct that looks like the proposed case
>> statement but can't quite be described in these terms. An example:
>>
>> given ($x) {
>> $y = 'a' when $a;
>> $y = 'b' when $b;
>> $y = 'c' when $c;
>> }
>>
>> The reason it's not exactly as described is that the "given" part and
>> the "when" parts are not connected syntactically. You can, for example,
>> have a set of "when" clauses in a "while" or "for" loop, and you don't
>> have to have any inside a "given" statement.
>>
>> It's an extraordinarily powerful construct, mainly due to the magic
>> that a "when" clause can do.
>
> The description of that feature in Perl isn't very reassuring.
>
> Quoted from:
> http://perldoc.perl.org/perlsyn.html#Switch-Statements
>
> <quote>
> Exactly what the EXPR argument to when does is hard to describe precisely,
> but in general, it tries to guess what you want done. Sometimes it is
> interpreted as $_ ~~ EXPR, and sometimes it does not. It also behaves
> differently when lexically enclosed by a given block than it does when
> dynamically enclosed by a foreach loop. The rules are far too difficult to
> understand to be described here.
> </quote>
>
>
> I don't doubt it can be wonderful or useful to some people, but I really
> doubt that it is a good idea do adopt a feature whose rules are described as
> "far too difficult to understand" in the language's documentation.
You mean in C? I was not proposing it for C! Perl programmers tend to
be rather relaxed people, and they'll just avoid this construct in
production code until the exact definition is pinned down. But in Perl,
the advantage of adopting a feature with evolving rules is that language
developers will get real freedback about real use cases. When the
semantics are finally cast in stone, the feature will be all the better
for it.
I've used it a few times just to get a job done fast, and it's very
expressive. I doubt any of these script will actually break down the
line because the "natural" uses are the ones that are going to be
preserved. It's the corner cases that are in flux.
> Maybe that kind of stuff is valued in the Perl community, but gratuituously
> adding features which are "far too difficult to understand", particularly
> when they don't provide anything which isn't already offered by features
> that exist for decades, only creates problems and needlessly complicates
> things.
You've subtly changed the meaning there! "far too difficult to
understand" is not the same as "far too difficult to understand to be
described here". There is a much fuller explanation elsewhere.
Perl did not have a switch statement, and I think it would have been
considered "underpowered" to add one that just compared expressions for
equality.
--
Ben.