In C or C++ this code invokes "undefined behavior" (behavior that is not defined by the C standard) because it relies on the order of evaluation of two sub-expressions that reside within the same sequence point. The == relational operator in C does not form a sequence point. But the = has a side effect. We cannot know (based on the C or C++ standards) what the order of evaluation will be when there is no sequence point defining the order. Therefore, we cannot know when the side effects will be applied. (See http://en.wikipedia.org/wiki/Sequence_point.)

With C or C++, when the behavior is not defined by the standard, it is quite possibly different between various implementations, and in any implementation, could be surprising.

Language lawyers in the C or C++ worlds love to shake their heads in condescending scorn when they see code that unknowingly invokes undefined behavior. Even with Perl, this is that sort of code; within the same sequence point, there is no formal definition of the order in which subexpressions will be evaluated. These subexpressions have side effects, and we cannot know when the side effects will take effect.

With Perl we can reason about what probably will happen because the implementation is often accepted as the definition. But eq still doesn't form a sequence point, and in the absence of an explicit rule, I think this is one to avoid trusting in real code. It's still pretty much "undefined behavior".


Dave


In reply to Re: Debating With Friends by davido
in thread Debating With Friends by exquisitemb

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.