Perl 5.20 and the fate of smart matching and given-when

PerlSmartmatch

Perl Problem Overview


I just installed Perl 5.18, and I get a lot of warnings like this,

given is experimental at .\[...].pl line [...].
when is experimental at .\[...].pl line [...].
Smartmatch is experimental at C:/strawberry/perl/site/lib/[...] line [...].

Looking into these warnings -- which I've never heard mentioned anywhere -- I was only able to find this in two places,

The Perl Delta still does the most to give mention as to what's happening with those features. It's halfway down buried in the pod,

> Smart match, added in v5.10.0 and significantly revised in v5.10.1, has been a regular point of complaint. Although there are a number of ways in which it is useful, it has also proven problematic and confusing for both users and implementors of Perl. There have been a number of proposals on how to best address the problem. 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. Warnings will now be issued when the parser sees ~~, given, or when.

I'm confused at how the most significant change in Perl in the past 10 years could be pulled. I've started using given, when, and smartmatch all over the place. Is there any more information about these futures? How is anyone finding them "confusing?" How are these features likely to change? Is there a plan to implement these features with a module?

Perl Solutions


Solution 1 - Perl

There are problems with the design of smart-matching. The decision of what any given TYPE ~~ TYPE should do is most often unobvious, inconsistent and/or disputed. The idea isn't to remove smart matching; it's to fix it.

Specifically, ~~ will be greatly simplified, as you can see in a proposal by the 5.18 pumpking. Decisions as to how two things should match will be done with helpers such as those that already exist in Smart::Match.

... ~~ any(...)

It is much more readable, much more flexible (fully extensible), and solves a number of problems (such as "When should X be considered a number, and when should it be considered a string?").

Solution 2 - Perl

Some insights might be gained by reading rjbs's proposed changes to smartmatch. He is the pumpking (Perl release manager) after all, so his comments and his view of the future is more relevant than most. There is also plenty of community comment on the matter; see here for instance. The 'experimental' status is in effect because, since things are likely to change in the future, it is responsible to inform users of that fact, even if we don't know what those changes will be.

Solution 3 - Perl

Well, that's what's said in the description of the patch that downgraded this set of features to experimental:

> The behavior of given/when/~~ are likely to change in perl 5.20.0: > either smart match will be removed or stripped down. In light of this, > users of these features should be warned. A category > "experimental::smartmatch" warning should be issued for these features > when they are used.

So while you can indeed turn these warnings off, with something like this (source):

no if $] >= 5.018, warnings => "experimental::smartmatch";

... it's just turning your eyes off the problem.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEvan CarrollView Question on Stackoverflow
Solution 1 - PerlikegamiView Answer on Stackoverflow
Solution 2 - PerlJoel BergerView Answer on Stackoverflow
Solution 3 - Perlraina77owView Answer on Stackoverflow