B
Bruce Cook
Peter said:Peter said:On 2009-01-29 22:54, Bruce Cook
Peter J. Holzer wrote:
(5) That "special false". I was going nuts trying to figure out
what was different between
[ '' in string context, 0 in numeric context ] [...]
Anyone who applies a numeric operator to a logical value and expects
consistent results is asking for trouble. [...]
In strongly-typed languages you would get a compiler or run-time
error/warning, however perl is a scripting language and is built to be
flexible, assumes you know what you're doing and will silently oblige
even the most horrendous abuses.
You actually have the same issue in C: false is defined as 0 and true
is !false.
However, in C, !0 is defined as 1.
This is where lots of people make mistakes in C.
You seem to be one of them.
if(a) is not the same as
if(a == !0)
I didn't say that. I said !0 is the same thing as 1 which means, that
if (a == !0)
is exactly the same as
if (a == 1)
While
if (a)
is the same thing as
if (a != 0)
Obviously (a != 0) and (a == 1) are not the same thing.
Yep, I missed the distinction in your original statement, I don't use the !0
construct as I find it rather pointless, so read it as "not zero".
Apologies
[...]
Then they were not C compilers.
Each of the operators < (less than), > (greater than), <= (less than
or equal to), and >= (greater than or equal to) shall yield 1 if the
specified relation is true and 0 if it is false.89) The result has
type int.
(ISO-9899:1999. A similar definition is in Appendix A, section
7.6 of K&R I, German translation (I don't have the original)
Certainly the first ISO draft said that, I don't think I remember it in K&R,
unfortunately I can't find my copy so I'll believe you.
So the result of
(-2 > 0)
would be -2 (i.e., true)? I think you should think about this a bit
more.
You're right, I think I was thinking of ==
I am quite sure of this as it was one of the first nasties that bit me when
I first started using C. Functioning code on 1 platform was just being
strange on another (probably astech C or BDS C). The development platform
was VMS and TOPS-10, which had a more modern C compiler (as modern as they
got in '83) and we were back-porting to CP/M and RSX systems.
If (-2 > 0) is true that's pretty broken by any measure. If a compiler
produces a different result than the C specification says it should, it
is still broken.
Certainly. I must admit to not being fully familiar with the standards.
When the original drafts were published the whole thing was a bit of a shit-
fight with various large organisations attempting to wedge their own self-
serving bits into the standard. I was interested in the process at the
start however as things got more stupid rapidly lost interest and just got
on with programming. I haven't really revisited since.
The first standard took a long time to appear and a very long time before we
saw any compilers that believed in the standards, much less obeyed them.
[...]
However, every version of perl (note: lower case 'p') since at least
perl4 (probably perl1) has always returned 1 as the true value for the
comparison operators (==, !=. <. >. <=, >=, eq, ne, lt, gt, le, ge) and
the logical negation (!, not). It is not clear to me whether this is
undocumented because @Larry want to keep the option of changing it one
day (in this case, why is the false value documented? The ''/0 mixture
is rather bizarre and it seems much more likely that one would like to
change that in some future revision of the language), or whether it is
simply undocumented because it's the same as in C and "people know that
anyway, so we don't have to write it down". (Perl documentation has
become more self-sufficient over the years, but originally it assumed a
lot of general Unix knowledge and there are still spots which are only
comprehensible if you have a background as a Unix/C programmer - this
might be one of them)
Yep, I see your point. I personally don't treat the result of a comparison
operator as a useful int so it's not something I irrelevant. Obviously
others like yourself who do program in this manner would find it frustrating
to have a commonly used tool not have an officially defined behavior.
The way perl was put together originally was fairly loose which gave perl
it's flexibility. This created quite a few language "features" the people
discovered and started using which either became official language
constructs or Larry had to break or implement properly/differently in
subsequent versions. There was also a lot of experimental features released
that often were replaced by a better way of doing it in a later revision
(objects anyone ?), all of which meant the documentation suffered quite a
bit.
As you say, more recently the documentation has become quite good, though
even now I find some things I know about that I can't find a good
explanation for in the docs.
Bruce