develooper Front page | perl.perl5.porters | Postings from December 2005

Re: switch

Thread Previous | Thread Next
From:
Damian Conway
Date:
December 11, 2005 16:21
Subject:
Re: switch
Message ID:
439C8B71.2010205@conway.org
Robin Houston wrote:

> when() has some additional magic, whereby if its argument is
> a comparison, a filetest, a sub/method call, or a regex match,
> then it matches just when its argument is true, instead of
> doing a smart match. This lets you do things like:
> 
>  given($age) {
>      when ($_ < 18) { print "Too young\n" }
>      when ($_ > 99) { print "Too old\n" }
>  }

In Perl 6 this would be written:

    given($age) {
        when {$_ < 18} { print "Too young\n" }
        when {$_ > 99} { print "Too old\n" }
    }

but that's not practical here, I suspect (due to look-ahead ambiguities with 
the anonymous hash syntax). Your alternative looks good to me.


> This is really a design question, and a thorny one at that.
> There's a big question mark in my mind over the right way
> to treat a blessed reference that has overloaded string/numeric
> values. (Should it be treated like a reference, or like a
> string/number?) One TODO item is to make it possible to
> overload smart match behaviour explicitly, using something
> like
> 
>   use overload '~~' => sub { ... };

This is how we expect people to resolve that same issue in Perl 6 (though, of 
course, the operator specification syntax is different).


> but what about:
> 
>   my $dt = [];
>   push @$dt, $dt;
>   given($dt) {
>       when ($dt) {
>           # Oh dear! An infinite loop
>       }
>   }
> 
> ?
> Of course we could detect loops explicitly, and that may well be
> the best thing to do.

When smart-matching two references, test first for referential equality and 
short-circuit if true.


> Anyway, there are a lot of decisions to be made about the way
> smart-matching should work, and I'd really appreciate some input.

FWIW, I'd like to see it follow:

     http://dev.perl.org/perl6/doc/design/syn/S04.html#Smart_matching

as closely as possible. And we'd greatly appreciate feedback if you find that 
specification ambiguous, unimplementable, or deficient in any way. ;-)

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