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

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

Thread Previous | Thread Next
From:
Aristotle Pagaltzis
Date:
November 28, 2015 08:15
Subject:
Re: [perl #126749] FR: get more benefit from ||, &&
Message ID:
20151128081459.GA83505@plasmasturm.org
* 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

-- 
say Just::another::Perl::hack::->${\sub{pop=~tr~:~ ~sr.","}}
#Aristotle Pagaltzis // <http://plasmasturm.org/>

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