Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3221442
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:52:35+00:00 2026-05-17T15:52:35+00:00

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

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:52:36+00:00Added an answer on May 17, 2026 at 3:52 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has 2 sections one for single line match and one for multi
I'm pretty experienced with Perl and Ruby but new to Python so I'm hoping
Perl is one of the things I never quite had the justification to get
How do you know whether a variable has been set at a particular place
I'm trying to understand a particular Perl code from vcake . Usually I find
I have a file which I use to centralize all strings used in my
This answer to Command line command to auto-kill a command after a certain amount
I have what is probably a really dumb grep in R question. Apologies, because
Requirements I have a very large CSV file to read. (about 3 GB) I
I'm learning Python, and I have a situation where I want to consume items

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.