Empty Parenthesis Evaluting to True -- WHY? 
Author Message
 Empty Parenthesis Evaluting to True -- WHY?


>I came across a piece of code and I can't figure out why it works, and
>it does indeed work.

Good catch.   You have described the situation exactly.  It is a weird
special case.

The rationale and the history come from the C language.  Here I'll
quote from "The C Programming Language"  (second edition) by Kernighan
and Ritchie, page 224:

        ... a statement

                for (expression1; expression2; expression3) statement

        is equivalent to

                while (expression2) {

        Any of the three expressions may be dropped.  A missing second
        expression makes the implied test equivalent to testing a
        non-zero constant.

That is, if you leave out the condition part of a "for(;;)", you get
an infinite loop.  And for many year people have written infinite
loops in C as:

        for (;;) {

Since Perl has the same for(;;) statement, it works the same way, and
for(;;) is an infinite loop in Perl.

Now since 'for' and 'while' are equivalent, they are implemented the
same way.  When Larry Wall wrote perl 1, back in 1987, he had a
choice:  He could follow the C syntax exactly, so that

        while () { ... }

would be a syntax error.  Or he could extend the equivalence of 'for'
and 'while' so that an omitted condition in a 'while' statement would
mean an infinite loop, just as the omitted condition in the 'for' loop
always had.  He chose the latter, to see if people would start using
'while ()' as an infinite loop.  They didn't.  (Larry says: "It's just
another one of those early generalizations that never quite got into
common use.  Ah, well.  Live and let learn.")

There was a discussion about this on p5p, the Perl developers' mailing
list, about a year and a half ago; you can see it at:


and ensuing messages, including some from Larry explaining why he
decided to do it that way.  One remark he made:

        "I remember being impressed with Ada because you could write
        an infinite loop without a faked up condition."      

($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print

Mon, 29 Sep 2003 08:56:42 GMT  
 Empty Parenthesis Evaluting to True -- WHY?

>I seem to recall it being a side effect of equivalence to

>   for (;;) { }

It was explicitly coded into parser on purpose.  It wasn't an accident.



($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print

Mon, 29 Sep 2003 09:13:33 GMT  
 [ 2 post ] 

 Relevant Pages 

1. Empty Parenthesis Evaluting to True -- WHY?

2. SetRange or Filter problem

3. Help - Delphi 16, Watcom SQL (NetWare) and ODBC

4. Problem with SVGA

5. Decompiling TP6

6. $@ true but empty?

7. Empty IF-TRUE-EXPR in ?:

8. $@ true, but empty?

9. Why is (?{ }) assertion always true?

10. Why are multiple zeroes true?

11. Why are files required to return true values?

12. Why Does This Return True?


Powered by phpBB® Forum Software