Results matching “smartmatch”

Perl 5 Porters Weekly: August 20-August 26, 2012

[ Cross posted from its home blog ]

Welcome to Perl 5 Porters Weekly, a summary of the email traffic on the perl-5-porters email list. Sorry I'm running a bit behind this week; there was really a tremendous volume of email this week, mostly about Ricardo's changes to smartmatch and the given/when syntax. Since that's the case, I'm going to put all of the non-smartmatch mail first, and a good sampling of the discussion around smartmatch at the end.

Topics this week include:

Why do you want new major features in core?

I've heard some people complain about 5.12 and 5.14 not adding many new major features. Compared to 5.10 that's certainly true, but is that a bad thing?

Let's be honest, many (most) prominent new features of 5.10 are failures:

  • Smartmatching? I think everyone agrees it is broken.
  • given/when is even worse as it's almost impossible to predict if it will use smartmatching or not.
  • Lexical $_? Mostly a new source of bugs, and the _ prototype is merely a hack to work around lexical $_ issues.
  • MAD? It never reached any usable form.
  • etcetera…

A lot of others are not flawed, but are so uncommon that I haven't seen them being used in any code. UNITCHECK, stacked filetests, no VERSION, the list goes on…

In the end, there are only two new features of 5.10 that I end up using all the time: say and defined-or. These two features have one thing in common: they are small and simple features that make daily programming easier. Likewise my favorite new feature in 5.14 is the /r modifier on s/// and tr///. I don't know how we managed to do without that, it makes so much code so much simpler. I want more of those features.

On the other hand, there's another group of new features that is just as important, but not nearly as visible. It's the under-the-hood or right on top of it. Few people know how the $^H and %^H changed in 5.10, but if you're writing pragmas you'll appreciate them. These are expert features that few people will use, but those few use them to write the modules on CPAN that everyone else uses. These features are just as important, if not more so: they lay the foundation for progress by enabling (competitive) evolution on CPAN.

It is important for this progress to happen not in core but on CPAN. Because if modules screw up they can be discarded and we can try to come up with something better (which you can't with the core). Because modules easily allow an allow an ecosystem of TIMTOWTDI. But most of all because the Perl community is awesome at creating modules.

Few people outside the echo chamber follow know Steven's work on a perl MOP, but that may become the most important development in Perl OO since the arrival of his Moose (also written by him). Likewise few people are using Zefram's awesome keywords API, but that is what will allow us to do Devel::Declare kind of stuff in a sane way, and may one day open up doors to macros. Few people know of the Unicode improvements by Karl Williamson and others that make Perl hands down the best language for Unicode processing.

We can't always know end-user's requirement in advance. That's why big core features should be open ended. Maybe that doesn't make for spectacular perldeltas, but it does lead to a better end result.

Smarter matching

Smartmatching is complicated. Anyone suggesting otherwise simply hasn't used it much. Something that appears to be as simple as when @array can have 5 rather different meanings depending on the value of $_, the two most important being «each element in @$_ matches the corresponding element in @array» and «$_ matches any of @array». This is madness. I believe there's a lot of value in being able to explicitly differentiate when list(@array) and when any(@array).

Hence Smart::Match. It's a module that does exactly that and more. Not only does it allow you to disambiguate smartmatches, it also allows you to use powerful new matches by using higher order matchers. For example it doesn't just allow you to match against string_length(4), but also string_length(even) or string_length(range(13,42)). This makes smartmatching a lot easier and saner than doing it raw.

Here's a stupid bug.

Suppose you want to check if a number is either 70 or 73, and your duct-tape-and-chewing gum production environment is stuck in the stone age without the smart-match operator.

You might be tempted to write foo() if $num =~ /^70|73$/, as I did. Oops. That will match 70 and 73, but also anything that ends in 73, like 173 or 273 or foo73. /^(?:70|73)$/ fixes it.

The moral of the story: life sucks without smartmatch.

  1 2 3 4 5 6 7

About coke

user-pic Perl 6 hacker