RE: Mysterious the ways: == and eq
by ZZamboni (Curate) on May 25, 2000 at 21:52 UTC
|
Perl is not typeless. It's only that its types are easily
(and in many cases automatically) converted from one to another.
It is precisely because of this flexibility that you need
different comparison operators. Because a scalar could be
interpreted as both a number and a strings, you need to tell
perl which basis you want to compare.
Let's say you have $a="1" and $b="000001". Numerically they are
the same, but for the purposes of your application those
leading zeros could be very important. Because perl has no
way of knowing how you want to compare those two variables,
you need to tell it by using the appropriate comparison operator.
--ZZamboni
| [reply] |
|
|
Well said. Although the language is typeless, it is critical that logical query expressions be type-specific. For example, it might be critical to tell if a string is, exactly, "1.0" (eg. for a version number). Of course, you can use /^1\.0$/ (I think that's right :) ) but that's an inefficient approach. For efficiency's sake, 'eq' is needed, and 'gt' and 'lt' have quite different meanings to > and <.
In perl, "1.2" > "1a", but "1.2" lt "1a". If I remember correctly :)
| [reply] |
RE: Mysterious the ways: == and eq
by t0mas (Priest) on May 25, 2000 at 23:13 UTC
|
Perl is not typeless,
it behaves like it does
so you can type less...
/brother t0mas
| [reply] |
RE: Mysterious the ways: == and eq
by merlyn (Sage) on May 26, 2000 at 01:46 UTC
|
Thus was spake...
For a typeless language...
Perl is anything but a typeless language.
- You can't store a hash into an array variable.
- You can't store an array into a scalar.
- You can't dereference a hashref as if it was a coderef.
- ... and so on.
I don't know where this "typeless" stuff comes from. Perl is very strongly typed. Just because "scalar" is a type that includes things that other languages call "strings", "characters", "numbers", and "references", doesn't mean that "scalar" isn't distinct type!
-- Randal L. Schwartz, Perl hacker | [reply] |
|
|
| [reply] |
|
|
while i agree with you, that perl is a very typed language...
the person is complaining not about the inability to compare
things of different types... what died here is the comparison
of a scalar against a scalar, the system died not because
we were comparing different types! The system died
because we were comparing the same type, with a different
value... that, you have to admit, on some level... seems
"bad"
| [reply] |
|
|
ok, that's the word i was looking for... orthagonality...
language fails the "test" of a good language (according
to my programming language theory book) at this point (
the eq == discrepancy) perl is NOT orthagonal...
| [reply] |
RE: Mysterious the ways: == and eq
by chromatic (Archbishop) on Jun 06, 2000 at 18:36 UTC
|
The reason Perl has things like this (operators that seemingly do duplicate things) is so that Perl hackers can give perl hints as to what the Right Thing is.
Though it often does the Right Thing even in ambiguous situations (is "001" supposed to be interpreted as a number or a string?), there are occasions when it needs explicit instructions. Things like eq and scalar and so forth let you be sure things are interpreted as you intend. | [reply] |
RE: Mysterious the ways: == and eq
by infoninja (Friar) on May 25, 2000 at 23:46 UTC
|
When they both are strings,
one and zero one differ
== and eq
| [reply] |
RE: Mysterious the ways: == and eq
by Anonymous Monk on May 26, 2000 at 15:37 UTC
|
It's precisely because perl is typeless that multiple comparison operators are needed. | [reply] |