Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

== and != don't work as expected

by esharris (Monk)
on Aug 09, 2004 at 14:18 UTC ( #381234=perlquestion: print w/replies, xml ) Need Help??

esharris has asked for the wisdom of the Perl Monks concerning the following question:

I read that == returns true if its operands are numerically equal.

However 'two' == 'two' executes a warn and returns a false value.

If "two" has a numeric interpretation, the preceding expression should return true, because numeric equality is reflexive.

If "two" does not have a numeric interpretation, then, IMHO, the preceding expression should die. Why did the developers decide that 'two' == 'two' should warn and return false?????? Is there a Pragma that will force such an expression to die? Under the current interpretation, it is difficult to detect errors in web applications. I.e., it can be the source of an obscure error.

Replies are listed 'Best First'.
Re: == and != don't work as expected
by davorg (Chancellor) on Aug 09, 2004 at 14:32 UTC
    $ perl -lwe 'print "two" == "two"' Argument "two" isn't numeric in numeric eq (==) at -e line 1. Argument "two" isn't numeric in numeric eq (==) at -e line 1. 1

    So it returns a true value.

    But that's because Perl tries to convert strings to numbers in order to do the numeric conversion. "two" has no obvious numeric equivalent so Perl uses zero.

    Perl rarely dies because it finds data in the wrong format. It tries hard to do what it thinks you want to do. This is usually seen as a feature.

    If it's essential that your program only compares actual numbers, then you should check them before you do the comparison.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: == and != don't work as expected
by Limbic~Region (Chancellor) on Aug 09, 2004 at 14:31 UTC
    esharris,
    I can't reproduce your problem
    $ perl -Mstrict -Mwarnings -e 'print "foo\n" if "two" == "two";' Argument "two" isn't numeric in numeric eq (==) at -e line 1. Argument "two" isn't numeric in numeric eq (==) at -e line 1. foo
    The warning is useful since you should be using eq there, but it does indeed return true because those strings do have numerical equivalence when the treated that way.

    Cheers - L~R

Re: == and != don't work as expected
by GreyGlass (Sexton) on Aug 09, 2004 at 15:31 UTC
    If you prefer fatal treatment, you can:

    use warnings FATAL => qw(numeric);

    Of course if you like something else, you can always get it by doing your favorite thing in a
    $SIG{__WARN__} handler.

Re: == and != don't work as expected
by esharris (Monk) on Aug 09, 2004 at 15:33 UTC
    My bad. I should have posted a coded example.

    I was confused when 'two' != 'one' returned a false value in my program. I agree that 'two' == 'two' returns a true value.

    I understand that this semantics is consistent.

    When developing a complex Perl program, I would prefer for 'two' != 'one' to die, rather than return false. IMHO, the current behavior is not always desirable.

    Sorry.

      Are you sure that you don't just want the string comparison operators - eq, ne, etc?

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

      So use fatal warnings instead. `perldoc perllexwarn'
      C:\>perl -Mwarnings=FATAL,all -e"print 1 if shift == shift" foo foo Argument "foo" isn't numeric in numeric eq (==) at -e line 1. C:\>perl -we"print 1 if shift == shift" foo foo Argument "foo" isn't numeric in numeric eq (==) at -e line 1. Argument "foo" isn't numeric in numeric eq (==) at -e line 1. 1

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

      The numeric value of the string "two" is zero (0)

      The numeric value of the string "one" is zero (0)

      Comparing the numeric values of the two strings, "two" and "one", shows they have the same numeric value. This is identical to the numeric value of "potato" ( and even of "potatoe" ), "Zambia", "infinity" and "TomDLux".

      If you want to compare the strings, use '=='. If you want to use numeric comparisons on strings, make sure they contain numbers.

      --
      TTTATCGGTCGTTATATAGATGTTTGCA

Re: == and != don't work as expected
by Vautrin (Hermit) on Aug 09, 2004 at 15:08 UTC
    Perl is a weakly typed language. Unlike C, for isntance, if you wanted to create a number then a string you could do:
    my $string = 1; $string = "code"; # we can reuse the variable name
    versus:
    char* mystring = "this is a C style string"; int myInt = 1; // Can't reuse variable names // plus need to declare the type
    You will find this paradigm called DWIM (Do What I Mean) by a lot of people. Basically, part of the price we pay for DWIM is that the interpreter doesn't choke and die on ambiguities, but makes a best guess.
      Perl is a weakly typed language.
      <mini_rant>

      No, it's not. It just doesn't associate the type with the name, as you might expect. Try coercing a hash to an array in place or treating a scalar as an array, for example.

      C's typing system is only slightly more advanced than bare assembly code.

      </mini_rant>
        Out of curiousity, can you name some weakly typed languages with your definition?

        Want to support the EFF and FSF by buying cool stuff? Click here.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (1)
As of 2022-05-19 03:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?