[Kc] proposal: functional C<if>

Don Ellis don.ellis at gmail.com
Wed Apr 25 16:50:00 PDT 2012


On Wed, Apr 25, 2012 at 4:27 PM, David Nicol <davidnicol at gmail.com> wrote:
> On Wed, Apr 25, 2012 at 3:34 PM, Jesse Luehrs <doy at tozt.net> wrote:
>> On Wed, Apr 25, 2012 at 02:58:07AM -0500, David Nicol wrote:
>>> On Tue, Apr 24, 2012 at 1:18 PM, Jesse Luehrs <doy at 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
>>
>> Sure, but if I didn't care about it being pretty, ?: already works too(:

Any thoughts on using whitespace to pretty it up a little? Might not
work so well on -e command line (or it might with the right
treatment), but looks fine in a script. I used this indentation in C
to make some sprintf conditionals more readable.

my $foo = $foo->bar
        ? $bar
        : $baz;

--Don Ellis


More information about the kc mailing list