[Israel.pm] What's new in Perl 5.10
gaal at forum2.org
Tue Dec 25 06:12:13 PST 2007
On Dec 25, 2007 3:14 PM, Jason Elbaum <jason.elbaum at gmail.com> wrote:
> > Smart Matching in Perl 5.10
> > http://www.szabgab.com/blog/2007/12/1198487688.html
The context in which this should be seen is as a sneak preview of Perl 6.
> I actually think this new feature is representative of one of Perl's
> design flaws.
> Larry Wall sees Perl as based on a number of design principles, most
> of which I agree are good guidelines for programming language design.
> But some of them tend to conflict with each other:
"The problem with being consistent is that there are lots of ways to
be consistent, and they're all inconsistent with each other" -- That
same Larry Wall
Larry's job is to find the happy harmony in the natural conflicts.
> 1. "Different things should look different. Similar things should look similar."
> -> http://www.perl.org.il/presentations/larry-wall-present-continuous-future-perfect/transcript.html
> 2. "Do What I Mean" or "Do the Right Thing."
> The problem is that doing "the right thing" requires the compiler to
> do *different* things with the same (or very similar) code, depending
> on its understanding of the "right" thing.
> In this case, we have a single operator ~~ which can either mean:
> - Do these numbers have the same value?
> - Do these strings have the same characters?
> - Do this number and this string represent more or less the same thing?
> - Does this string match this regular expression?
> - Is this element found in this array?
> - Is this element one of the keys of this hash?
> - Do these arrays have the same elements?
> - Do these hashes have the same keys?
> - etc., etc., etc.
> Keeping track of all these cases and how they interact is not
> necessarily intuitive, and will likely call for frequent reference to
> the documentation. That's because they're fundamentally doing
> *different* things, even though they all look the same.
True. But it'd be laborious to do anything else; at least this saves a
lot of work _some_ of the time. If Larry's guesses were right, in many
cases, this will do the right thing. If not, as an _author_ you have
1. do it the laborious way, as you'd have done in another language.
2. locally override the way smartmatch works. You can do that; Perl 6
makes this easy and encourages it. It's a big deal in the language,
and bears repeating: you can change the language in parts, but your
effect is always lexically scoped.
As a _reader_ of Perl 6 code, I agree the problem exists. Any large
language is going to have it. (Any overly small language has it too,
only the problem is then transported to different levels.)
At least the default semantics _are_ well defined.
> And when you say it checks if two arrays have the "same" elements,
> does it check whether they are identical? Or whether they succeed when
> smart-matched themselves?
In Perl 6, the smart matching is not recursive.
> Finally, note that with eq for strings, == for numbers and ~~ for
> smart matching, Perl still lacks any single operator which can tell
> you if two scalars (let alone two arrays or hashes) are simply
> identical - contain the same exact string and/or number and/or undef
> and/or reference. (That may be an impossible mission because of
> automagical string/number conversion in scalars - another example of
> DWIM conflicting with different things looking different.)
In Perl 6, this would be the "eqv" operator. (For immutable values,
"===" would also work).
> Unfortunately, the "I" in "DWIM" refers not to the programmer but to
> the compiler. I'd rather the compiler always do something
> well-defined, obvious and consistent, rather than applying complex
> heuristics which ultimately make "simple" code do hard-to-guess
> The more I use Perl, the more I conclude that DWIM is a language
> design anti-pattern. It's a shame, since on most of his design
> principles, Larry is right on the mark.
I'd suggest reading Synopsis 3, in particular the sections on smart
matching and comparison semantics. There's a big difference in how p5
and p6 are specified.
> Jason Elbaum
> Perl mailing list
> Perl at perl.org.il
Gaal Yahas <gaal at forum2.org>
More information about the Perl