Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

using smartmatch for range matching

by perltux (Monk)
on Nov 25, 2023 at 16:03 UTC ( [id://11155820]=perlquestion: print w/replies, xml ) Need Help??

perltux has asked for the wisdom of the Perl Monks concerning the following question:

I use smartmatch for checking whether an integer number is within a certain range, like this:

if ( $number~~[1416..1494] ) { do whatever }

But since i upgraded to a recent Perl version i'm getting a "Smartmatch is deprecated" warning.

Why would such a useful operator be depreciated?

Do I really have to replace '~~' with:

if ( $number>=1416 and $number<=1494 ) { do whatever }

Is there a more elegant solution similar to the smartmatch example?

Also is this depreciation of smartmatch definitive or is there a chance that the Perl devs will change their mind and keep it?
Perl is great because of these brilliant operators, removing them removes a bit of the uniqueness of Perl.

Replies are listed 'Best First'.
Re: using smartmatch for range matching
by tybalt89 (Monsignor) on Nov 25, 2023 at 17:44 UTC

    There is  1416 <= $number <= 1494 in later perls.

      IMHO it's also MUCH more efficient than the smartmatch, which will check against every element of a potentially huge temporary array, instead of just the boundaries.

      Edit

      And certainly better readable.

      Saying so, having an in LIST operator wouldn't harm, but that's not the demonstrated use case.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

        Sorry, but this sticks out like a sore thumb!

        And certainly better readable.

        Either:

        And certainly better readability.

        Or maybe:

        And certainly more readable.

               :-)
        
        Naked blocks are fun! -- Randal L. Schwartz, Perl hacker
      Thank you, that's a good alternative, but i will have to figure out the minimum Perl version required to use this, does anyone know when it was introduced?

        perl5320delta

Re: using smartmatch for range matching
by ikegami (Patriarch) on Nov 26, 2023 at 17:51 UTC

    If you want to keep the $number ~~ [1416..1494] notation without the inefficiencies of it and the problems inherent with using smart-matching, you could use

    in_range( $number, 1416, 1494 )

    But I would use the following instead:

    1416 <= $number <= 1494

    Also is this depreciation of smartmatch definitive or is there a chance that the Perl devs will change their mind and keep it?

    Its buggy due to a broken design, and it's being removed in 5.42.

    5.10: Somehow introduced despite the known broken design.
    5.10.1: Backwards incompatible change because it was so bad in 5.10.0. But didn't address the broken design. (That would be impossible without completely changing what the operator does.)
    5.16: Becomes experimental (in the documentation).
    5.18: Using experimental features now warn.
    5.38: Becomes deprecated. It was announced it would be removed in 5.42.

    There's a clear path towards removal. And the above severely understates the chaos the feature has caused. The feature and chaos resulted in the creation policies to make new features experimental, backed with the creation of a formal system for warning when experimental features are used. They also caused the policies for backwards compatibility and the breakage thereof to be formalized. Finally, the above doesn't mention the various pushes to have the failed experiment removed, attempts that were stymied by the presence of a large user base.

      > in_range( $number, 1416, 1494 )

      What module does it come from?

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

        It's a trivial sub you can write yourself:

        sub in_range { $_[1] <= $_[0] <= $_[2] }
Re: using smartmatch for range matching
by hippo (Archbishop) on Nov 25, 2023 at 16:22 UTC

    Yes, smartmatch is deprecated. It is going away entirely in Perl 5.42. Follow the link for reasoning.

    Is there a more elegant solution similar to the smartmatch example?

    Well, there are plenty of modules you could use. Perhaps Number::Range? I have not used it but it might fit the bill.

    Note also that although deprecated and depreciated are similar words they do not mean the same thing.


    🦛

      I try to avoid using non core modules as much as possible as they would make my programs less portable (most Linux distros offer only a limited selection of packaged Perl modules and using cpan is not an option if I want to package my program for a Linux distro).

      So to reformulate my question: Is there a more elegant solution similar to the smartmatch example and without using non-core modules?


      Quoting from the link you kindly provided with regards to deprecation of smartmatch:

      " its behavior is noticed for being unnecessarily complex and unintuitive"

      It looks like the "dumbing down everything" disease has sadly reached Perl too... :-(

Re: using smartmatch for range matching
by eyepopslikeamosquito (Archbishop) on Nov 25, 2023 at 22:12 UTC

    But since i upgraded to a recent Perl version i'm getting a "Smartmatch is deprecated" warning. Why would such a useful operator be depreciated?

    I keep a list of references on this topic: Switch/smartmatch References

    👁️🍾👍🦟

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11155820]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2025-06-17 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.