develooper Front page | perl.perl5.porters | Postings from February 2019

Re: New defined-match operator

Thread Previous | Thread Next
From:
Uri Guttman
Date:
February 21, 2019 17:41
Subject:
Re: New defined-match operator
Message ID:
b8b852b9-b9f6-b5bc-1648-3de1900ed09b@stemsystems.com
On 2/21/19 12:31 PM, Dan Book wrote:
> On Thu, Feb 21, 2019 at 7:13 AM David Cantrell <david@cantrell.org.uk 
> <mailto:david@cantrell.org.uk>> wrote:
>
>     We all know how convenient it is to write this:
>
>       $foo //= 'bar';
>
>     instead of:
>
>       $foo = 'bar' unless(defined($foo));
>
>     Well, wouldn't it be nice to be able to write this:
>
>       if($foo //~ /bar/)
>
>     instead of:
>
>       if(defined($foo) && $foo =~ /bar/)
>
>     Thoughts?
>
>
> While I don't dislike the concept, the //= operator is just an 
> assignment form of the // operator and expands to $foo = $foo // 'bar' 
> (with $foo only being evaluated once). //~ does not fit in this 
> construction. Perhaps it could be something similar to the idea of the 
> maybe-arrow operator - ?=~
>
what about this:

     ($foo //= '') =~ /bar/ ;

the goal is to eliminate the undef warning. you can put any default 
value for the null string. this is easy to write and read and no new op 
is needed.

uri


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