develooper Front page | perl.perl5.porters | Postings from September 2016

Re: pounding the smartmatch drum

Thread Previous | Thread Next
From:
Eirik Berg Hanssen
Date:
September 6, 2016 15:12
Subject:
Re: pounding the smartmatch drum
Message ID:
CAHAeAG6ynhN1jedseNe7yS72s1jmOT4nHX=cYj0vbsue1keByw@mail.gmail.com
On Tue, Sep 6, 2016 at 4:51 PM, Paul "LeoNerd" Evans <leonerd@leonerd.org.uk
> wrote:

> On Mon, 5 Sep 2016 21:23:54 -0500
> David Nicol <davidnicol@gmail.com> wrote:
>
> > I agree. We already have a way to "ask the lhs whether it matches" and
> > that system is called method dispatch.
> >
> > sub UNIVERSAL::DumbMatch{0}
> > sub DumbMatchOnOdd::DumbMatch{ [0,1]->[$_[1] % 2] }
> > ...
> > $SomeObjectOrOther->DumbMatch($PretzelPattern) and say "got a
> > pretzel";
>
> Well, almost. That is a bit dirty and pollutes the UNIVERSAL namespace.
>
> Nicer would be if perl had notations for "try to call this method but
> if it doesn't exist, yield some sentinel falsey value like undef or
> empty list".


  Oh, and yield the same falsey value for a lhs that isn't a blessed
reference, right?

  ;-)

  … okay, I'm sure someone reading this might not be aware that they
already know this, so …

  eval { $SomeThing->DumbMatch($PretzelPattern }


  (Aside from $SIG{__DIE__}, damn it, the problem with this is if the
string value of $SomeThing is a class name.  But that problem was there
ever since the method dispatch hammer was chosen for this particular nail.)


Eirik

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About