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

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

Thread Previous | Thread Next
From:
Father Chrysostomos
Date:
September 16, 2012 17:14
Subject:
Re: given/when/~~ "final" thoughts (ha ha ha)
Message ID:
8279AEAF-E426-4430-BB0A-6E2D6A1B8C1B@cpan.org

On Sep 16, 2012, at 2:07 PM, Jesse Luehrs wrote:

> 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.

Making it work wouldn’t be hard.  That would also make lexicals visible, as in when{my $x}{$x}.  I can’t say I’m very fond of the idea.  But I am not opposed to it per se.

> 
>>>  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?

eq wouldn’t hurt.  Neither would croaking.  I am not attached to either.

> 
>> 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?


From the commit message:

given now responds to last and next.

break only breaks out of given, and does not complain about an
inner foreach.

when and default do an implicit regular next, not a ‘special’ next
that only breaks out of foreach() and given.


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