in reply to while ()

This isn't related to being able to transform for into while, and appears to be a deliberate choice. The parser injects a constant "1" if either while() or the middle part of for(;;) are empty: perly.y:
/* Loops: while, until, for, and a bare block */ loop : label WHILE '(' remember texpr ')' mintro mblock cont { PL_copline = (line_t)$2; $$ = block_end($4, newSTATEOP(0, $1, newWHILEOP(0, 1, (LOOP*)Nullop, $2, $5, $8, $9, $7))); + } ... | label FOR '(' remember mnexpr ';' texpr ';' mintro mne +xpr ')' mblock /* basically fake up an initialize-while lines +eq */ { OP *forop; PL_copline = (line_t)$2; forop = newSTATEOP(0, $1, newWHILEOP(0, 1, (LOOP*)Nu +llop, $2, scalar($7), $12, $10, $9)); if ($5) { forop = append_elem(OP_LINESEQ, newSTATEOP(0, ($1?savepv($1):N +ullch), $5), forop); } $$ = block_end($4, forop); } ... /* Boolean expression */ texpr : /* NULL means true */ { (void)scan_num("1", &yylval); $$ = yylval.op +val; } | expr ;
("remember" starts a lexical scope, but doesn't use any tokens.)

AIUI, while () would be illegal if it had said

loop : label WHILE '(' remember expr ')' mintro mblock cont
instead.

Replies are listed 'Best First'.
Re^2: while ()
by jdhedden (Deacon) on Jun 08, 2005 at 12:41 UTC
    The code in ysth's post is completely definitive, of course, and can be further verified by using the Perl bytecode compiler (B::Bytecode). If you execute the following:
    perl -MO=Bytecode,-H,-ofor_loop -e 'for (;;) {}' perl -MO=Bytecode,-H,-owhile_loop -e 'while () {}' perl -MO=Bytecode,-H,-owhile1_loop -e 'while (1) {}'
    and then diff the resulting files (for_loop, while_loop and while1_loop), you will find that they are all completely identical. This means that all three are exactly equivalent as far as code execution goes.