Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Update to smartmatch

by stevieb (Abbot)
on Dec 23, 2017 at 20:19 UTC ( #1206122=perlnews: print w/replies, xml ) Need Help??

Hi all,

The smartmatch (operator, ~~) has been a fiasco for quite a long time, and caused much heated debate for so many years now that I consider it a historical piece of 'work'. Those who have paid attention to this feature knows what I speak of.

This is just to bring attention to those who have interest, that there have once again been changes made in the most recent dev release (5.27.7). Personally, this has been such a long running debate that I don't think there's ever going to be a way to make everyone happy, so I digress.

Aside from that, Happy Holidays to all of my fellow Monks, and thank you for accepting and having me as one of your peers, friends and oftentimes I nearly feel, colleagues for yet another year.


Replies are listed 'Best First'.
Re: Update to smartmatch
by Laurent_R (Canon) on Dec 25, 2017 at 21:27 UTC
    Thanks stevieb for bringing up this issue.

    I am quite happy that I've never used the smartmatch operator and the given construct for any production code in Perl 5 (although not for the reason that I was clever, but for the reason that our production machines run very old versions of Perl).

    I don't know all the details, so I may be wrong, but it seems to me something is going wrong there. I very much respect Leon T., and I also respect Sawyer X., but it appears that the P5P process has gone somewhat amok.

    Anyway, I think that anyone concerned with this issue will have some interest reading these detailed comments from brian d foy:

      I don't know all the details

      In a few words,

      1. given, lexical $_ and smartmatching were added to Perl without enough vetting in 5.10.

      2. In fact, smartmatching was so bad that it was tweaked in 5.10.1.

      3. People starting noticing the problems with them, which resulted into a new policy: New features to be added to Perl would first be included as experimental features. By default, these warn when used.

      4. given, lexical $_ and smartmatching were retroactively converted into experimental features in 5.18, effectively deprecating existing uses of them.

      5. Lexical $_ was rolled back in 5.24.

      6. given and smartmatching are being revamped for 5.28. It will still be an experimental feature, but it now has a chance of being released as normal part of Perl.

        Thank you, ikegami. In fact, I knew about the history of smartmatch and given (as well as lexical $_). I was only saying that I don't know the details on the most recent developments as described by Leon and brian (i.e. your point 6) and the discussions around these. But it's probably good to retell the story for the benefits of those monks who would not know or not remember.

      I also have not ever used "smartmatch" and friends. I have never encountered it in code I've worked on at various employers, and that's neither because they were so clever, nor because they were all using old perls. It's because of the incredible confusion and instability around the "features," and also because no good use case for using them has been presented. What exactly is the problem that it's supposed to solve, that cannot be elegantly (enough) solved with normal loop and conditional constructs?

      I will not shed a tear if smartmatch/given/etc. are reimplemented so code that uses them breaks (I expect that CPAN modules that break will be fixed), or if it completely goes away for now (which I think is the most sensible suggestion in the p5p thread that encapsulates the current debate).

      However, I am alarmed at the process with which the changes have been already merged into bleadperl, as highlighted by LeonT and brian d foy. If that is the way things are going to get done, I fear that Perl will come to be viewed -- correctly -- as an unstable mess where there are no adults in charge.

      In my opinion, that of a journeyman, workday Perl programmer, at the root of all this was the fiction that "Perl6" was a real thing in 2007, and the precipitous rush to implement many of its "features" in Perl v.5.10. Damian Conway and people who, like him, are outlandishly clever but also suffer from an excess of hubris, should be kept far away from the core of Perl!

      The way forward always starts with a minimal test.

        I also have not ever used "smartmatch" and friends. I have never encountered it in code I've worked on at various employers,

        I think you're trying to prove they're not a desired features, but it's far more likely that you've worked a companies that sensibly avoid using experimental (unstable) features in production code.

        It's because of the incredible confusion and instability around the "features,"

        There's no confusion; smartmatching is an experimental feature, not a stable feature. This will still be the case in 5.28. Continue not using it unless you want to be a beta tester.

        I am alarmed at the process with which the changes have been already merged into bleadperl

        Aye, there were problems, which were resolved by the creation of the policy and ability to make new features experimental.

        On rare occasions I've found it very convenient for debugging purposes, such as when I want to quickly compare two arrays.

        If I recall correctly, last time I used it I was "cleaning up" some old code and I wanted a quick way to double check that the output array from the old code and the output array from the new code were exactly the same.

        Just another Perl hooker - My clients appreciate that I keep my code clean but my comments dirty.
Re: Update to smartmatch
by ikegami (Pope) on Dec 26, 2017 at 20:20 UTC

    Apparently, a lot of code on CPAN uses the experimental features, and are breaking as the result of the change you mentioned. Things are very much still quite in flux.

      imho, smartmatch is "experimental", will likely never be stable, and should not ever be used in any code on the CPAN. If it is used in any authored code, the documentation should state it clearly, and warn loudly.

Re: Update to smartmatch
by choroba (Bishop) on Feb 23, 2018 at 16:46 UTC
    Post mortem from the Pumpking.
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Update to smartmatch
by njahnke (Novice) on Feb 18, 2018 at 01:51 UTC
    I use smartmatch to check a string against a list of precompiled regexen. I need to see if ANY of the regexen matches. If one does (I don't care which), then I do something with the capture group on the next line. That part is always the same - I just don't know which regex (if any) is going to match the input string. I don't know how to match a string against ANY regex in a list of them without smartmatch without using a loop construct with flow control. The difference is so big that I go out in public with `no warnings 'experimental::smartmatch'` at the top of my code just for this one use case.
      Just join the regexes into one large regex. Use | to separate them, and use branch reset (?|...) to start counting the capture groups from 1 in each alternative.
      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: smartmach updated? or something? perl5-porters did something somewhere and some porter commented about it
by Anonymous Monk on Dec 24, 2017 at 03:27 UTC

    Hooray ... smartmatch has been gutted again?

    Talk about out of context ...

    Hehe, even the OP not interested to find out what the where what what all

      Not "again". This is the promised guttingfixing of the experimental feature.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlnews [id://1206122]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2018-07-17 22:35 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (378 votes). Check out past polls.