develooper Front page | perl.perl5.porters | Postings from August 2012

Re: fixing smartmatch just hard enough (and when, too)

Thread Previous | Thread Next
From:
Damian Conway
Date:
August 23, 2012 21:19
Subject:
Re: fixing smartmatch just hard enough (and when, too)
Message ID:
CAATtAp46Y9p9GLmhd6Efib=kqy4=2hQ4uoUB2Ci5dEnTSUNHKg@mail.gmail.com
Darth Aureous suggested:

> I think the challenge inherent in the ambiguous RHS case is improved if we
> give people a way to detect it and respond appropriately.  E.g.
>
>     when { $lhs ~~ $rhs // $lhs ~~ "$rhs" } { ... }

Oh, that's so evil! dagolden++

Though maybe I'd be inclined to SWIM there:

     when { $_ ~~ $rhs // $_ eq $rhs } { ... }

Which you can also write (albeit with much less Sith) like so:

    when (any $rhs, "$rhs") {...}

And that scales rather nicely to a general fallback idiom:

    #         Try ==  then eq  then =~
    when (any 0+$rhs, "$rhs",  qr/$rhs/) {...}

Of course, normally you'd just write:

    # Try ==, then eq, then =~
    when (0+$rhs)   {...}
    when ("$rhs")   {...}
    when (qr/$rhs/) {...}

but if the block was complex, and identical in all three cases,
it would be much easier to have only a single 'when'. :-)

Damian

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