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

Re: [perl #108286] Wishlist: Overridable keywords

Thread Previous | Thread Next
From:
David Nicol
Date:
April 25, 2012 00:58
Subject:
Re: [perl #108286] Wishlist: Overridable keywords
Message ID:
CAFwScO_BMFpwmjgjQR2-Jw19Av7PZsLAuSagdMHeXyypLt8iXw@mail.gmail.com
On Tue, Apr 24, 2012 at 1:18 PM, Jesse Luehrs <doy@tozt.net> wrote:

> I have on occasion wished for a more functional programming-style if
> statement:
>
>  my $foo = if ($foo->bar) { $bar } else { $baz };
>
> It is a lot more readable than ?: when the expressions get more
> complicated.

C<do> is very handy and will let you do that:

$ perl -le 'my $X = do { if ( 0 ) { 27 } else { 33 }};print $X'
33

$ perl -le 'my $X = do { if ( 1 ) { 27 } else { 33 }};print $X'
27

-- 
In this document, the key words "MUST", "MUST NOT", "REQUIRED", "SHALL",
"SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and
"OPTIONAL" are to be interpreted using situational ethics.

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