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

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

Thread Previous | Thread Next
From:
Brad Baxter
Date:
August 31, 2012 06:58
Subject:
Re: fixing smartmatch just hard enough (and when, too)
Message ID:
CANBtU9YroBgHJD0HObGN9gLqnNbppcM=p1Y69R7HB7+PVvya9w@mail.gmail.com
On 8/26/12, Father Chrysostomos <sprout@cpan.org> wrote:

...

> The third one actually has nothing to do with smart match at all.  It’s a
> ‘when’ problem.  If we really really don’t want to have to write
> when($_==3), then we could copy the precedent set by flipflop and split.
>
>   if (1..2)           means  if ($.==1 .. $.==2)
>   split ' '           is special
>   $x = ' '; split $x  does something different
>
> and implement two syntactic special cases:
>
>   # literal numbers
>   when(0)
>   when(1)
>   when(0xff)
>   when(0b101110)
>   when(023476)
>   # literal strings
>   when('foo')
>   when("foo")
>   when(q "foo")
>   when(qq "foo")
>
> and *nothing* else.  Everything else would be a boolean, including
> when($foo).  Anything else is madness; it really is.
>
> Then we can just deprecate ~~.
>
> Those who say they want
>
>   when(num(3))
>
> can write
>
>   sub num { $_ == $_[0] }
>
> instead of
>
>   sub num { my $arg = shift; sub { $_[0] == $arg } }
>
> which I think is an improvement.
>
> But that doesn’t work with lexical $_, so let’s deprecate that, too.
>
> (And make when do a simple next, and make given respond to next.)

+1 re: when

I don't have an opinion about deprecating smartmatch; I don't expect
to ever use it.

If I know 'when' will never do an implicit smartmatch, then I'll use 'when'.

I'd be happy if

    when ( ... ) { ... }

were simply a synonym for

    if ( ... ) { ... ;next}

but the above special cases are fine with me--I'd probably use them.

BTW, perlsyn in Perl 5 version 16.0 documentation
(http://perldoc.perl.org/perlsyn.html#Switch-Statements) says,

At the end of all when blocks, there is an implicit next. You can
override that with an explicit last if you're interested in only the
first match alone.

Note: "implicit next". Am I missing something? Does when already do a
simple next?

-- 
Brad

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