Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Disadvantage of using 'eq' operator to compare numbers.

by paragkalra (Scribe)
on Jan 01, 2010 at 12:15 UTC ( #815175=perlquestion: print w/ replies, xml ) Need Help??
paragkalra has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,

I have one basic question. Although it may sound silly.

What is the disadvantage of using 'eq' operator to compare two numbers instead of '=='

I mean what bugs are likely to creep in.

Comment on Disadvantage of using 'eq' operator to compare numbers.
Re: Disadvantage of using 'eq' operator to compare numbers.
by marto (Chancellor) on Jan 01, 2010 at 12:46 UTC
Re: Disadvantage of using 'eq' operator to compare numbers.
by holli (Monsignor) on Jan 01, 2010 at 12:51 UTC
    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.

      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". ;-)
Re: Disadvantage of using 'eq' operator to compare numbers.
by biohisham (Priest) on Jan 01, 2010 at 12:54 UTC
    Strings can look like 0 to the == operator and you can get into uncalled for hithces if you swap the string and numeric operators:
    $apples = "apples"; $oranges = "oranges"; if($apples == $oranges){ print "$apples and $oranges are the same"; }else { print "$apples and $oranges are not the same"; }



    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: Disadvantage of using 'eq' operator to compare numbers.
by JavaFan (Canon) on Jan 01, 2010 at 14:20 UTC
    $_ = "Today is 01 Jan 2010"; my ($dayofmonth) = /(\d+)/; print $dayofmonth eq 1 ? "ok 1\n" : "not ok 1\n"; print $dayofmonth == 1 ? "ok 2\n" : "not ok 2\n"; __END__ not ok 1 ok 2
Re: Disadvantage of using 'eq' operator to compare numbers.
by ikegami (Pope) on Jan 01, 2010 at 22:18 UTC
    If you want one to be equal to one, you want the numerical operators:
    >perl -le"print 1 == '1.0' ?1:0" 1 >perl -le"print 1 eq '1.0' ?1:0" 0

    If you want two to be less than ten, you want the numerical operators:

    >perl -le"print 2 < 10 ?1:0" 1 >perl -le"print 2 lt 10 ?1:0" 0
      If you want two to be less than ten, you want the numerical operators:
      You've got a typo there... s/numerical/string/ (The examples still demonstrate it correctly.)

      The thinko was mine, not ikegami's. My comment was incorrect and adds nothing to the discussion; please reap.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://815175]
Approved by FunkyMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-09-02 04:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (19 votes), past polls