views:

208

answers:

4

Perl has a conditional operator that is the same a C's conditional operator.

To refresh, the conditional operator in C and in Perl is:

(test) ? (if test was true) : (if test was false)

and if used with an lvalue you can assign and test with one action:

my $x=  $n==0 ? "n is 0" : "n is not 0";

I was reading Igor Ostrovsky's blog on A neat way to express multi-clause if statements in C-based languages and realized this is indeed a "neat way" in Perl as well.

For example: (edit: used Jonathan Leffler's more readable form...)

# ternary conditional form of if / elsif construct:
my $s=
      $n == 0     ? "$n ain't squawt"
    : $n == 1     ? "$n is not a lot"
    : $n < 100    ? "$n is more than 1..."
    : $n < 1000   ? "$n is in triple digits"
    :               "Wow! $n is thousands!" ;  #default

Which reads a LOT easier than what many would write in Perl: (edit: used cjm's more elegant my $t=do{ if }; form in rafi's answer)

# Perl form, not using Switch or given / when
my $t = do {
    if    ($n == 0)   { "$n ain't squawt"        }
    elsif ($n == 1)   { "$n is not a lot"        }
    elsif ($n < 100)  { "$n is more than 1..."   }
    elsif ($n < 1000) { "$n is in triple digits" }
    else              {  "Wow! $n is thousands!" }
};

Are there any gotchas or downside here? Why would I not write an extended conditional form in this manner rather than use if(something) { this } elsif(something) { that }?

The conditional operator has right associativity and low precedence. So:

a ? b : c ? d : e ? f : g

is interpreted as:

a ? b : (c ? d : (e ? f : g))

I suppose you might need parenthesis if your tests used one of the few operator of lower precedence than ?:. You could also put blocks in the form with braces I think.

I do know about the deprecated use Switch or about Perl 5.10's given/when constructs, and I am not looking for a suggestion to use those.

These are my questions:

  • Have you seen this syntax used in Perl?** I have not, and it is not in perlop or perlsyn as an alternate to switch.

  • Are there potential syntax problems or 'gotchas' with using a conditional / ternary operator in this way?

  • Opinion: Is it more readable / understandable to you? Is it consistent with Idiomatic Perl?

-------- Edit --

I accepted Jonathan Leffler's answer because he pointed me to Perl Best Practices. The relevant section is 6.17 on Tabular Ternaries. This allowed me to investigate the use further. (If you Google Perl Tabular Ternaries, you can see other comments.)

Conway's two examples are:

my $salute;
if ($name eq $EMPTY_STR) {
    $salute = 'Dear Customer';
}
elsif ($name =~ m/\A ((?:Sir|Dame) \s+ \S+)/xms) {
    $salute = "Dear $1";
}

elsif ($name =~ m/([^\n]*), \s+ Ph[.]?D \z/xms) {
    $sa1ute = "Dear Dr $1";
}
else {
    $salute = "Dear $name";
}

VS:

           # Name format...                            # Salutation...
my $salute = $name eq $EMPTY_STR                       ? 'Dear Customer'
           : $name =~ m/ \A((?:Sir|Dame) \s+ \S+) /xms ? "Dear $1"
           : $name =~ m/ (.*), \s+ Ph[.]?D \z     /xms ? "Dear Dr $1"
           :                                             "Dear $name"
           ;

My conclusions are:

  • Conway's ?: example is more readable and simpler to me than the if/elsif form, but I could see how the form could get hard to understand.

  • If you have Perl 5.13.1, use my $t=do { given { when } }; as an assignment as rafi has done. I think given/when is the best idiom now, unless the tabular ternary format is better for your particular case.

  • If you have Perl 5.10+ use given/when in general instead of Switch or if you need some sort of case type switch.

  • Older Perl's, this is a fine form for simple alternatives or as an alternate to a case statement. It is better than using Switch I think.

  • The right to left associativity means the form is evaluated bottom to top. Remember that when using...

+18  A: 

I've seen this idiom used in perl. As the ternary operator ? : is, well.. an operator, it's documented in perlop, not perlsyn.

In my eyes, it is sort of idiomatic Perl, but the main purpose of this in Perl seems to avoid the lack of a proper switch statement while not writing huge if/else-cascades. However, this has been fixed years ago in perl 5.10.0. These days I can't see many reasons for not writing the above as this, which appears to be far more readable than (ab)using the ternary:

given ($n) {
    when (0)         { $t = "$_ ain't squawt"        }
    when (1)         { $t = "$_ is not a lot"        }
    when ($_ < 100)  { $t = "$_ is more than 1..."   }
    when ($_ < 1000) { $t = "$_ is in triple digits" }
    default          { $t = "Wow! $_ is thousands!"  }
}

or, as of perl 5.13.1, even as:

my $t = do {
    given ($n) {
        when (0)         { "$_ ain't squawt"        }
        when (1)         { "$_ is not a lot"        }
        when ($_ < 100)  { "$_ is more than 1..."   }
        when ($_ < 1000) { "$_ is in triple digits" }
        default          {  "Wow! $_ is thousands!" }
    }
};

Another alternative would be something like this, wich works on all perl versions:

my @cases = (
    [sub { $_ == 0 },   sub { "$_ ain't squawt"        }],
    [sub { $_ == 1 },   sub { "$_ is not a lot"        }],
    [sub { $_ < 100 },  sub { "$_ is more than 1..."   }],
    [sub { $_ < 1000 }, sub { "$_ is in triple digits" }],
    [sub { 1 },         sub { "Wow! $_ is thousands!"  }],
);

for my $case (@cases) {
    local $_ = $n;
    next unless $case->[0]->();
    $t = $case->[1]->();
    last;
}

While this avoids both using huge if/elsif/else-cascades, and doesn't need features of recent perls, it's probably not worth the effort for this simple example. However, I can very much see an approach like this being useful with lots of conditions and with the constraint of wanting to support old perls.

(Also note that your initial example doesn't handle $n being smaller than zero or not being a number at all.)

Note from cjm: You can also do this in all versions of Perl 5:

my $t = do {
    if    ($n == 0)   { "$n ain't squawt"        }
    elsif ($n == 1)   { "$n is not a lot"        }
    elsif ($n < 100)  { "$n is more than 1..."   }
    elsif ($n < 1000) { "$n is in triple digits" }
    else              {  "Wow! $n is thousands!" }
};
rafl
cjm
Good point. Thanks! :-)
rafl
It's not *the* ternary operator, it's *a* ternary operator. It's *the* conditional operator.
Ether
Oh, you're absolutely right. I guess I mixed up those names because `?:` is the only ternary oparator in perl (is it?). Edited accordingly. Thanks!
rafl
The form of `test ? true : false` is unambiguously called **the conditional operator** in C, C++, C#, Java, Javascript, Perl and PHP. In vb.net it is called 'ternary if' or `iif()`. Python calls it `Conditional expressions (sometimes called a “ternary operator”)`. In my mind its *classification* is a ternary operator called `the conditional operator`.
drewk
@rafi: As stated in my post, I am aware of the `given/when` construct in Perl 5.10 and later, and please put that aside for now. You call the form I used as '(ab)using the ternary'. Why is this use abusing the conditional operator? When you look at Jonathan Leffler's post, do you think that form is hard to read?
drewk
Yes, I do think it's harder to read than many of the alternatives, and I also believe it's harder to write. You're using `?:` and basically turni it into a very deeply nested control structure. With some clever formatting is isn't quite as bad as it could be, but still at least my brain works better with "flatter" structures. The last alternative I gave, which resembles Lisp's `cond` form, works better for me, and also scales better for cases that aren't as trivial as your example. It's really quite close to using a hash as a look-up table, which you also often do to avoid `if/else`-cascades.
rafl
+11  A: 

The layout shown for the conditional operator is hard to read. This is more like what I recall Perl Best Practices recommending:

my $s = $n == 0   ? "$n ain't squawt"
      : $n == 1   ? "$n is not a lot"
      : $n < 100  ? "$n is more than 1..."
      : $n < 1000 ? "$n is in triple digits"
      :             "Wow! $n is thousands!";  # default...

And there are times when it is better to use a more compact notation with the if notation, too:

  if    ($n == 0)   { $t = "$n ain't squawt";        }
  elsif ($n == 1)   { $t = "$n is not a lot";        }
  elsif ($n < 100)  { $t = "$n is more than 1...";   }
  elsif ($n < 1000) { $t = "$n is in triple digits"; }
  else              { $t = "Wow! $n is thousands!" ; }  

Both these reformattings emphasize the similarity of the various sections of the code, making it easier to read and understand.

Jonathan Leffler
I like your formatting for both alternatives better, and the conditional form you have still reads more easily to me than the if / elsif. What do you think?
drewk
@drewk: they are both fairly readable; the conditional operator form has a single assignment so it is clear that a single value is assigned, whereas with the if-chain version one of the actions could be '`$l = "something";`' and the typo in the variable name might go unspotted for a while. So, yes, I think there are advantages to the conditional operator - but it isn't a dreadfully strong preference.
Jonathan Leffler
+1  A: 

And yet another way!

my $t = sub {
    return "$n ain't squawt"        if $n == 0;
    return "$n is not a lot"        if $n == 1;
    return "$n is more than 1..."   if $n < 100;
    return "$n is in triple digits" if $n < 1000;
    return "Wow! $n is thousands!";
}->();

I touch on this in a couple of blog posts I did:

/I3az/

draegtun
+1  A: 

I've seen the chained conditionals quite a bit, used it sometimes, and hated it always. It's handy but ugly unless you go to extremes to format it and simplify the interstitial expressions.

They aren't so hard to understand once you've run across them a couple of times and realize it is an idiom. It's easier to understand a proper switch statement though. There's also less of a chance of misplacing a colon and messing everything up in a hard-to-spot way.

brian d foy