Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

while() {}

by kappa (Chaplain)
on Feb 26, 2008 at 18:44 UTC ( [id://670351]=perlquestion: print w/replies, xml ) Need Help??

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

Good day to all! Does anyone know why while() {} is not a syntax error but an endless loop (as per B::Deparse, it's actually while(1) {}? This is not a practical question, but one completely out of curiousity. Meanwhile, if() {} is a syntax error.
--kap

Replies are listed 'Best First'.
Re: while() {}
by ysth (Canon) on Feb 26, 2008 at 19:11 UTC
Re: while() {}
by ikegami (Patriarch) on Feb 26, 2008 at 19:01 UTC

    Probably because while() and for(;;) are also infinite loops in C (according to MS's compiler and gcc).

    I use for (;;) regularly and read it as "for ever". Perl6 will provide loop { } for that.

    Update: I can't reproduce my earlier results. I must have confused my for (;;) test results for those of the while () test.

      I get this trying to compile while() ; with gcc 4.1.3:
      % LANG=C cc 1.c 1.c: In function 'main': 1.c:2: error: expected expression before ')' token
      --kap
        Yes - it's a syntax error with gcc-3.4.5 and Visual Studio 7.0, too. (I think ikegami is mistaken about this.)

        Cheers,
        Rob
Re: while() {}
by casiano (Pilgrim) on May 03, 2008 at 10:37 UTC
    There is some inconsistency here?.
    While while() as prefix is an infinite loop:
    $ perl -MO=Deparse -e 'while() { print "1\n" }' while (1) { print "1\n"; } -e syntax OK
    as suffix never executes:
    $ perl -MO=Deparse -e 'print "1\n" while ()' print "1\n" while (); -e syntax OK

    Casiano

      The syntax for the suffix notation is
      EXPR while EXPR;
      and not
      EXPR while ( EXPR );

      Therefore, you are comparing apples and oranges.
      In while() { print "1\n" }, the expression is "".
      In print "1\n" while ();, the expression is "()", the empty list, which is false in boolean context.

      You're right about there being an inconsistency, though, just not the one you mentioned.

      >perl -MO=Deparse -e"while() { print 1 }" while (1) { print 1; } -e syntax OK >perl -MO=Deparse -e"print 1 while;" syntax error at -e line 1, at EOF -e had compilation errors.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 12:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found