Btw I can confirm that I get the same result on Windows XP TinyPerl 5.8.

My suspicion is that the answer may lie in the precedence of operators. The = seems to be evaluated first, and then the the ?: group second. Am I right?

We could confirm this by doing : $z = 1; $x = 1; $z = $x ? $z = 2 : $z *= 9; print $z;

Now, the output is not 1, it's not 9 but 18. How does it become 18? The fact that it's 18 seems to suggest that first the $z = 2 gets evaluated, and then immediately after that the $z *= 9 gets evaluated. (Note: the = sign and *= are on the same level of precedence.)

Edit: Another interesting code is $z = 1; print $z *= 2, $z *= 9; # This prints 1818 instead of 218

Edit: I thought I was onto something, but apparently not. I'm just even more lost now. I did this :

my $A = 1; my $B = 7; my $C = 5; my $D = 0; $D = $A ? $B *= 2 : $C *= 9; print "A=$A B=$B C=$C D=$D";

And the output is..... :drum roll:

A=1 B=126 C=5 D=126

Okay. I give up. I'm lost! Maybe we discovered a bug? How does $B become 126? Lol


In reply to Re: Evaluating the condition ($x ||= $y) by harangzsolt33
in thread Evaluating the condition ($x) by syphilis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.