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
| [reply] [d/l] |
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.
| [reply] [d/l] |
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.
| [reply] [d/l] [select] |
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. | [reply] |
|
| [reply] |
|
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. |
| [reply] [d/l] |
|
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
| [reply] |
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. | [reply] [d/l] [select] |
|
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>
| [reply] [d/l] [select] |
|
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.
| [reply] |
|
|
|