develooper Front page | perl.perl5.porters | Postings from March 2020

Dumbmatch operators [was Re: Thoughts in the direction of a coretry/catch syntax]

Thread Previous | Thread Next
From:
Paul "LeoNerd" Evans
Date:
March 14, 2020 14:30
Subject:
Dumbmatch operators [was Re: Thoughts in the direction of a coretry/catch syntax]
Message ID:
20200314143032.155d3ec7@shy.leonerd.org.uk
At risk of derailing the try/catch discussion, I'll reply anyway but as
a different subject line


On Fri, 13 Mar 2020 12:13:55 +0100
"H.Merijn Brand" <h.m.brand@xs4all.nl> wrote:

> I have a few real-world switches, all use *just* 4 cases
> 
>   when (undef)

Oh. Hrm; none of my four mentioned operators so far (eq, ==, isa, =~)
are nicely defined for undef. They can't be used to distinguish undef
from defined values.

As a side-note, I have sometimes wanted an "undef-aware-eq"
operator that is true for undef/undef, false without warning for
undef/defined, and otherwise acts exactly like eq for two defined
values. Also for ==.

I wonder if the invention of those kinds of operator would help my
dumbmatch idea...

>   when ("fixed-string")
>   when (42)
>   when (qr{^foo})
> 
> My longest production-case switch/given is 206 lines long, it has
> • 33 when (qr{foo | bar})
> • 19 when ("foo")

Ah; a single "given" that contains both regexp pattern and string
equality tests? I guess that particular mixture might be more common
than the more general case. It can easily be supported by just
rewriting them as

  when (qr{\Afoo\z})

but it might be nice to see if a better solution could be found.

> That script has 2600 lines of embedded documentation and about 800
> lines of code and comments: this means that 25% of the script is taken
> by the given/when block


Overall: I'm not quite sure what you're advocating for or against here.
You've supplied some information but I'm unsure what to do with it.

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

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