<p>This snippet of Perl code in my program is giving the wrong result.</p>

<pre><code>$condition ? $a = 2 : $a = 3 ;
print $a;
</code></pre>

<p>No matter what the value of <code>$condition</code> is, the output is always 3, how come?</p>

[{'answer_id': 9011, 'author': 'Pat', 'author_id': 238, 'author_profile': 'https://Stackoverflow.com/users/238', 'pm_score': 7, 'selected': True, 'text': '<p>This is explained in the Perl <a href="http://perldoc.perl.org/perlop.html#Conditional-Operator" rel="noreferrer">documentation</a>.</p>\n\n<p>Because of Perl operator precedence the statement is being parsed as</p>\n\n<pre><code>($condition ? $a= 2 : $a ) = 3 ;\n</code></pre>\n\n<p>Because the ?: operator  produces an assignable result, 3 is assigned to the result of the condition.</p>\n\n<p>When $condition is true this means ($a=2)=3 giving $a=3</p>\n\n<p>When $condition is false this means ($a)=3 giving $a=3</p>\n\n<p>The correct way to write this is</p>\n\n<pre><code>$a = ( $condition ? 2 : 3 );\nprint $a;\n</code></pre>\n\n<p>We got bitten by this at work, so I am posting here hoping others will find it useful.</p>\n'}, {'answer_id': 21800, 'author': 'Tithonium', 'author_id': 2425, 'author_profile': 'https://Stackoverflow.com/users/2425', 'pm_score': 4, 'selected': False, 'text': "<p>Just to extend the previous answer... If, for whatever reason, the assignments need to be part of the conditional, you'd want to write it thusly:</p>\n\n<pre><code>$condition ? ($a=2) : ($a=3);\n</code></pre>\n\n<p>This would be useful if you're assigning to different variables based on the condition.</p>\n\n<pre><code>$condition ? ($a=2) : ($b=3);\n</code></pre>\n\n<p>And if you're choosing the variable, but assigning the same thing no matter what, you could even do this:</p>\n\n<pre><code>($condition ? $a : $b) = 3;\n</code></pre>\n"}, {'answer_id': 43697, 'author': 'Jagmal', 'author_id': 4406, 'author_profile': 'https://Stackoverflow.com/users/4406', 'pm_score': 0, 'selected': False, 'text': "<p>One suggestion to Tithonium's answer above:</p>\n\n<p>If you are want to assign different values to the same variable, this might be better (the copy-book way): </p>\n\n<p>$a = ($condition) ? 2 : 3;</p>\n"}, {'answer_id': 46956, 'author': 'theorbtwo', 'author_id': 4839, 'author_profile': 'https://Stackoverflow.com/users/4839', 'pm_score': 5, 'selected': False, 'text': '<p>Once you have an inkling that you might be suffering from precedence problems, a trick to figure out what Perl thought you meant:</p>\n\n<pre><code>perl -MO=Deparse,-p -e \'$condition ? $a= 2 : $a= 3 ; print $a;\'\n</code></pre>\n\n<p>In your case, that\'ll show you:</p>\n\n<pre><code>(($condition ? ($a = 2) : $a) = 3);\nprint($a);\n-e syntax OK\n</code></pre>\n\n<p>...at which point you should be saying "oh, that explains it"!</p>\n'}, {'answer_id': 73753, 'author': 'raldi', 'author_id': 7598, 'author_profile': 'https://Stackoverflow.com/users/7598', 'pm_score': 2, 'selected': False, 'text': "<p>Because of Perl operator precedence the statement is being parsed as:</p>\n\n<pre><code>($condition ? $a = 2 : $a ) = 3 ;\n</code></pre>\n\n<p>Because the ?: operator produces an assignable result, 3 is assigned to the result of the condition.</p>\n\n<p>When $condition is true this means $a=2=3 giving $a=3</p>\n\n<p>When $condition is false this means $a=3 giving $a=3</p>\n\n<p>The correct way to write this is</p>\n\n<pre><code>$a = $condition ? 2 : 3;\n</code></pre>\n\n<p>In general, you should really get out of the habit of using conditionals to do assignment, as in the original example -- it's the sort of thing that leads to Perl getting a reputation for being write-only.</p>\n\n<p>A good rule of thumb is that conditionals are only for simple values, never expressions with side effects. When you or someone else needs to read this code eight months from now, would you prefer it to read like this?</p>\n\n<pre><code>$x &lt; 3 ? foo($x) : bar($y);\n</code></pre>\n\n<p>Or like this?</p>\n\n<pre><code>if ($x &lt; 3) {\n  $foo($x);\n} else {\n  $bar($y);\n}\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/9009', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/238/']