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 8340889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:18:23+00:00 2026-06-09T05:18:23+00:00

I have the following code: # List of tests my $tests = [(system_test_builtins_sin, system_test_builtins_cos,

  • 0

I have the following code:

# List of tests
my $tests = [("system_test_builtins_sin", "system_test_builtins_cos", "system_test_builtins_tan")];

# Provide overrides for certain variables that may be needed because of special cases
# For example, cos must be executed 100 times and sin only 5 times.
my %testOverrides = (
    system_test_builtins_sin => {
        reps => 5,
    },
    system_test_builtins_cos => {
        reps => 100,
    },
);

my %testDefaults = (
    system_test_reps => 10,
);

# Execute a system tests
foreach my $testName (@$tests)
{
    print "Executing $testName\n";
    my $reps;

    if (exists $testOverrides{$testName}{reps})
        { $reps = $testOverrides{$testName}{reps}; }
    else
        { $reps = $testDefaults{system_test_reps}; }
    print "After long if: $reps\n";
    exists $testOverrides{$testName}{reps} ? $reps = $testOverrides{$testName}{reps} : $reps = $testDefaults{system_test_reps};
    print "After first ternary: $reps\n";
    exists $testOverrides{$testName}{reps} ? $reps = $testOverrides{$testName}{reps} : print "Override not found.\n";
    print "After second ternary: $reps\n";
}

This gives the following output:

Executing system_test_builtins_sin
After long if: 5
After first ternary: 10
After second ternary: 5
Executing system_test_builtins_cos
After long if: 100
After first ternary: 10
After second ternary: 100
Executing system_test_builtins_tan
After long if: 10
After first ternary: 10
Override not found.
After second ternary: 10

This output is most unexpected! I don’t understand why the first ternary seems to always be executing the “if false” clause. It is always assigning a value of 10. I also tried changing the “false” clause to $reps = 6, and I saw that it always got the value of 6. Why does the ternary’s logic depend on the content of the third (if false) clause?

  • 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-06-09T05:18:25+00:00Added an answer on June 9, 2026 at 5:18 am

    Here’s a simpler script that illustrates the problem:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $x;
    
    1 ? $x = 1 : $x = 0;
    print "Without parentheses, \$x = $x\n";
    
    1 ? ($x = 1) : ($x = 0);
    print "With parentheses, \$x = $x\n";
    

    It produces this output:

    Without parentheses, $x = 0
    With parentheses, $x = 1
    

    I’m not sure that the relationship between assignment and ?: can be complete explained by operator precedence. (For example, I believe C and C++ can behave differently in some cases.)

    Run perldoc perlop and search for “Conditional Operator”, or look here; it covers this exact issue (more concisely than I did here).

    In any case, I think that using an if/else statement would be clearer than using the ?: operator. Or, since both the “true” and “false” branches assign to the same variable, a better use of ?: would be to change this:

    exists $testOverrides{$testName}{reps}
        ? $reps = $testOverrides{$testName}{reps}
        : $reps = $testDefaults{system_test_reps};
    

    to this:

    $reps = ( exists $testOverrides{$testName}{reps}
              ? testOverrides{$testName}{reps}
              : $testDefaults{system_test_reps} );
    

    But again, the fact that I had to wrap the line to avoid scrolling is a good indication that an if/else would be clearer.

    You might also consider using the // operator, unless you’re stuck with an ancient version of Perl that doesn’t support it. (It was introduced by Perl 5.10.) It’s also known as the “defined-or” operator. This:

    $x // $y
    

    is equivalent to

    defined($x) ? $x : $y
    

    So you could write:

    $reps = $testOverrides{$testName}{reps} // $testDefaults{system_test_reps};
    

    This doesn’t have exactly the same semantics, since it tests the expression using defined rather than exists; it will behave differently if $testOverrides{$testName}{reps} exists but has the value undef.

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

Sidebar

Related Questions

I have the following code (correct for my simple tests) for a linked list
I have the following code: List<int> intList = new ArrayList<int>(); for (int index =
I have the following code: List<T> list = new List<T>(); IEnumerable<T> query1 = ...
I have the following code: List<HtmlMeta> metas = new List<HtmlMeta>(); foreach (Control c in
I have the following C# code: List<int[,]> l1 = new List<int[,]>(); l1[0] = new
I have the following code: SPList list = web.Lists[this.ListName]; SPListItem item = list.Items.Add(); now
I have the following code: public List<IWFResourceInstance> FindStepsByType(IWFResource res) { List<IWFResourceInstance> retval = new
I have the following code below for list into master page <div id=header> <ul>
I have the following code which takes List of boolean as a parameter then
I have following Code package cyclist.project; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException;

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.