Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Disadvantage of using 'eq' operator to compare numbers.

by holli (Abbot)
on Jan 01, 2010 at 12:51 UTC ( [id://815180]=note: print w/replies, xml ) Need Help??


in reply to Disadvantage of using 'eq' operator to compare numbers.

Not silly.
C:\>perl -e "print 12 == 0xC" 1 C:\>perl -e "print 12 eq 0xC" 1
What the f…?


holli

You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re^2: Disadvantage of using 'eq' operator to compare numbers.
by afoken (Chancellor) on Jan 01, 2010 at 14:20 UTC

    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.

    Alexander

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://815180]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 21:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found