Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Looking at print 12 == 0xC and print 12 eq 0xC with the eyes of a parser, I see the following:

  • Compare two values using numeric compare (first case, ==) or string-compare (second case, eq)
  • LHS value is not a string (no quotes, no bareword rules apply), but a decimal number literal (12), with a value of 12 (decimal)
  • RHS value is not a string (no quotes, no bareword rules apply), but a hexadecimal (0x) number literal (0xC), also with a value of 12 (decimal).
  • In the first case, both values are already numbers, no need to convert. Both values are constant and equal (12). In the second case, both values have to be converted to strings. After that, both values are constant and equal ("12").
  • Optimizing leaves a single true value (1).
  • Call the print function with that value.

This works exactly the same with octal constants (014), and makes the code look even more confusing because of the "innocent" zero: Both print 12 == 014 and print 12 eq 014 end printing a 1.


Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^2: Disadvantage of using 'eq' operator to compare numbers. by afoken
in thread Disadvantage of using 'eq' operator to compare numbers. by paragkalra

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (5)
    As of 2019-02-22 01:45 GMT
    Find Nodes?
      Voting Booth?
      I use postfix dereferencing ...

      Results (115 votes). Check out past polls.

      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!