develooper Front page | perl.perl5.porters | Postings from December 2017

Re: Revisiting smart match

Thread Previous | Thread Next
December 7, 2017 20:44
Re: Revisiting smart match
Message ID:
Morse, Richard E.,MGH wrote:
>Will the old smartmatch code be available via experimental?

No.  It's a failed experiment, and we're getting rid of it.  However,
it's not difficult to write code for the new smartmatch (whichever
version of it we ultimately settle on) that will work identically under
the old smartmatch.

The key is that the new smartmatch is driven by overloading of the ~~
operator by its right-hand operand, and such overloading is also a rule
honoured by the old smartmatch.  If all your smartmatch operations have
a reference to such an overloaded object on the rhs, then that goes most
of the way to making smartmatch code behave identically on both versions.
You also need to avoid the implicit enreferencement of the old smartmatch,
because that's probably going away in favour of regular scalar context.

For your match-any use case, you need some supporting code amounting
to this:

    package MatchAny {
        use overload "~~" => sub {
            foreach(@{$_[0]}) {
                return 1 if $_[1] ~~ $_;
            return 0;
    sub any { bless(\@_, "MatchAny") }

Then you write "$a ~~ any($b, $c, $d)" in place of "$a ~~ [$b, $c, $d]".


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About