develooper Front page | perl.perl5.porters | Postings from November 2015

Re: [perl #126749] FR: get more benefit from ||, &&

Thread Previous | Thread Next
From:
Abigail
Date:
November 28, 2015 10:53
Subject:
Re: [perl #126749] FR: get more benefit from ||, &&
Message ID:
20151128105331.GA16091@almanda.fritz.box
On Sat, Nov 28, 2015 at 09:14:59AM +0100, Aristotle Pagaltzis wrote:
> * KES <kes-kes@yandex.ua> [2015-11-27 19:40]:
> > When we return list while list is required in this case we will have
> > these benefits:
> 
> Just use list assignment in scalar context like you did with the `if`:
> 
> >     @x ||= qw/ s o m e d e f a u l t s /; # ('s','o'...'s') if @x is empty
> 
>       @x || (@x = qw/ s o m e d e f a u l t s /);
> 
> >     @x = $str =~ /^(\d)(\d)(\d)$/ || (); # (7,5,3) if $str eq '753', NOT (3)
> 
> This is nonsensical. Just leave out the `|| ()`.
> 
> (You don’t write `$x = $y // undef;` either, unless you’re hungover.)
> 
> >     @x = fn_this() || fn_that(); # faster: only one call to fn_this
> >     @x = fn_this() ? fn_this() : fn_that() ; # slower: fn_this called twice
> 
>       (@x = fn_this()) || (@x = fn_that()); # only one call to fn_this


I typically write the latter as:

    @x = fn_this ();
    @x = fn_that () unless @x;



Abigail

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