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

Re: given/when/~~ "final" thoughts (ha ha ha)

Thread Previous | Thread Next
From:
Jesse Luehrs
Date:
September 16, 2012 14:07
Subject:
Re: given/when/~~ "final" thoughts (ha ha ha)
Message ID:
20120916210737.GI11137@tozt.net
On Sun, Sep 16, 2012 at 01:01:31PM -0700, Father Chrysostomos wrote:
> Ricardo Signes wrote:
> > The last thing I'd said about smartmatch was:
> > 
> >   $x ~~ undef                                                                   
> >   $x ~~ $overloaded_object                                                      
> >   $x ~~ sub {}                                                                  
> >   $x ~~ regex                                                                   
> >   ...or fail                                                                    
> > 
> > ...with when:
> > 
> >   when ("foo") # str eq
> >   when (12345) # num ==
> >   when ($x)    # ~~
> >   when { ... } # block evaluates true
> 
> I discovered a gotcha with that:
> 
>     given(42) {
>       when {/4(.)/} {
>         print $1, "\n"; # prints nothing
>       }
>     }
> 
> This would need to be documented.

Is there a reason it couldn't be made to work instead? This seems like
it could be useful enough to special-case, even if it wouldn't otherwise
work.

> >   when breaks the enclosing topicalizer
> 
> Define topicalizer.  Does it include while(<>)?
> 
> > 
> > I wanted to post that this is a design that would get accepted.  The
> > reservation of the final case, rather than eq/==, in smart match, lets us
> > figure out whether future work on improving num-v-str can be put to work here.
> 
> Have you noticed the sprout/smartpatch branch?
> 
> I found Smylers’ argument (<20120905143040.GZ1742@stripey.com>) convincing so I implemented it like this:
> 
>  $x ~~ undef
>  $x ~~ $overloaded
>  $x ~~ sub{}
>  $x ~~ qr//
>  $x ~~ $anthing_else # fall back to eq
> 
> But that makes ~~[] do the wrong thing silently in existing code, so adding this before the last case would improve it:
> 
>  $x ~~ $other_ref # croak

What about globs?

> And when() just does smartmatch.
> 
> So now we have three variations.  I’m fine with any of them, and they are *really* easy to implement.  So which will it be?
> 
> Two things I’m not volunteering to do are:
>   • Coordinate with autodie maintainers about getting it adjusted.
>     These two files use smartmatch or when:
>       cpan/autodie/lib/Fatal.pm
>       cpan/autodie/t/exceptions.t
>   • Write the documentation (don’t forget to warn about $1 being
>     unavailable in when {/(1)/} { $1 })
> 
> And what do you think of the new whirled order for
> given/when/last/next on that branch?  Also, should break and when
> produce exiting warnings (they do not currently)?

How do given/when/last/next work on that branch?

-doy

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