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

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

Thread Previous | Thread Next
Damian Conway
August 23, 2012 21:19
Re: fixing smartmatch just hard enough (and when, too)
Message ID:
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'. :-)


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