Ben Morrow said:
Quoth Rainer Weikusat said:
The actual text says
Smart match, added in v5.10.0 and significantly revised in
v5.10.1, has been a regular point of complaint.
[...]
It is clear that smartmatch is almost
certainly either going to change or go away in the
future. Relying on its current behavior is not recommended.
[...]
http://perldoc.perl.org/5.18.0/perldelta.html#The-smartmatch-family-of-features-are-now-experimental
AIUI, this does not state that the idea to introduce a real switch/ case
statement into Perl was 'a rather bad one'
You are using quote marks. Who are you quoting?
If in doubt, an invisible 'Greg KH' who has no problems with claiming to
be "The Greatest Programmer On Earth" and considering switch-statements
so incomprehensible that they have to be replaced at the same time and
any other dedicated member of the software section of the "There's
nothing worthwhile of being stated which can't be expressed clearly by
a string of differently accentuated 'fucks'"-tribe.
[...]
Well, in principle that's all that smartmatch is supposed to be doing:
equality or regex match or set match, as appropriate. In practice this
falls down at the first hurdle: Perl 5 has *two* equality operators, and
it matters which you use. (It also doesn't have a well-defined
reification of 'a class', making is-a tests equally problematic.)
I was writing about 'equality' and possibly, some notion of set
matches. I didn't include regexes because these can already be matched
against $_ without writing the '$_ =~'-part every time. I was also being
intentionally vague on 'set matches' because this was supposed to
include the possibility of something with a much more limited scope than
the 14 different set matches documented for perl 5.10.1 smart matching.
I'll address the 'equality' issue below.
If you can define sensible and useful semantics for 'when', then ~~ can
be defined to do the same thing.
That would hardly be worthwhile.
[...]
OTOH, given/when without some sort of smart matching just isn't that
useful.
Trying to be so ueber-smart that the final result is nothing because the
problem invariably diffuses into a cloud of nebolous opinions which all
have something going for and against them it isn't particular smart in
the end. "Do something less ambitious for now and leave the complicated
stuff for the future" is IMHO a better approach. Eg, a C
switch-statement requires all cases to be integer constants. That's
already useful when dealing with functions returning some discrete
values and possibly, a default case, eg, fork or <=> or anything which
returns 'magic numbers' to indicate different things. 'Equality' than
becomes == and if this doesn't do what is desired, another construct has
to be used. Going one step further, one could allow 'string literals'
(including interpolation) as well. 'Equality' than mean 'use == if it is
an integer constant and eq otherwise'. Additionally dealing with literal
regexes shouldn't be a big problem.
This would cover almost everything I've been using given/when for so
far and would still go beyond what is provided by the most 'current'
version of Java. I've actually used functions a la
sub eql($)
{
return $_ == $_[0];
}
or
sub equal($)
{
return $_ eq $_[0];
}
in the past in 'switch statements implemented as for loop' to avoid
repeating the '$_' (of course, I want to use that, that's why I wrote
for (something) {} in the first place) and the same comparison operator
every time.
Another simplistic addition would be to allow 'range operator
statements' in order to match a sequence of integers or literal strings.
(given doesn't even alias, making it's most obvious use, as an
equivalent to VB's 'with', impossible.)
AFAIK, with-statemens have existed in Pascal since "the dawn of times"
(relative to the age of VB, that is) ...