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

Re: smartmatch needs your eyes

Thread Previous | Thread Next
From:
H.Merijn Brand
Date:
September 12, 2015 06:25
Subject:
Re: smartmatch needs your eyes
Message ID:
20150912082443.06979f7e@pc09.procura.nl
On Fri, 11 Sep 2015 23:25:04 +0200, Aristotle Pagaltzis
<pagaltzis@gmx.de> wrote:

> * H.Merijn Brand <h.m.brand@xs4all.nl> [2015-09-10 17:45]:
> >     $x ~~ 42        -->     $x == 42        ???
> >     $x ~~ "foo"     -->     $x eq "foo"     ???  
> 
> In $x ~~ $y with $y eq 42 you cannot tell whether the user wanted the
> smartmatch to mean $x == 42 or $x eq 42. You can only tell that when the
> user has given you a literal 42 vs a literal '42'.
> 
> So this can only work with literals. Therefore it would appear to be
> useless: $x ~~ 42 with a literal 42 will never mean anything other than
> $x == 42, so you can just as well write $x == 42 yourself. And it gets
> worse: $x eq 42 is shorter and easier to type than $x ~~ '42' !
> 
> However, there is one case for it: if `when` is defined in terms of
> smartmatch, then these rules would allow writing e.g.
> 
>     given ($mode) {
>         when ('enable')  { ... }
>         when ('disable') { ... }
>         # ...
>     }
> 
> That’s syntax I don’t really want to lose, and it would be nicer to say
> that `when (EXPR)` is sugar for `if ($_ ~~ EXPR) { ...; break }` instead
> of making `when` have its own very special cases over and above what ~~
> does.
> 
> Regards,

This given/when example is exactly why I added those two lines, and is
also why I have no problem with the corner case being invalid.

The (ridiculous) example I have in real life is a perl script that acts
as a 'lp' wrapper for HP printers that is 2900 lines long (including
the full documentation so I can ship it as a single file) and has a 

    foreach my $opt (@opt_o) {
        given ($opt) {
            when ("A4") {
                }
            when (/^(?: tra?y ([0-9]))$/x) {
                }

with 50 "when" entries and only string and regex cases.

-- 
H.Merijn Brand  http://tux.nl   Perl Monger  http://amsterdam.pm.org/
using perl5.00307 .. 5.21   porting perl5 on HP-UX, AIX, and openSUSE
http://mirrors.develooper.com/hpux/        http://www.test-smoke.org/
http://qa.perl.org   http://www.goldmark.org/jeff/stupid-disclaimers/

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