[prev in list] [next in list] [prev in thread] [next in thread]
List: perl5-porters
Subject: Re: [perl #126749] FR: get more benefit from ||, &&
From: Abigail <abigail () abigail ! be>
Date: 2015-11-28 10:53:31
Message-ID: 20151128105331.GA16091 () almanda ! fritz ! box
[Download RAW message or body]
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
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic