while ( ) and until ( )

J

John Nurick

I'm trying to understand why 'while ()' behaves like 'while (1)', but
'until ()' doesn't behave like 'until (1)'. So far - maybe because of
clumsiness with perldoc - I can't find it documented.

This compiles and runs without warnings:
perl -e "use warnings; while () {$x++; print $x; last if $x==9; }"
123456789

while this won't compile:
perl -e "use warnings; until () {$x++; print $x; last if $x==9; }"
syntax error at -e line 1, near "() "
syntax error at -e line 1, near "; }"
Execution of -e aborted due to compilation errors.

Could someone please point me to an explanation?

TIA
John
 
T

Tassilo v. Parseval

Also sprach John Nurick:
I'm trying to understand why 'while ()' behaves like 'while (1)', but
'until ()' doesn't behave like 'until (1)'. So far - maybe because of
clumsiness with perldoc - I can't find it documented.

This compiles and runs without warnings:

123456789

while this won't compile:

syntax error at -e line 1, near "() "
syntax error at -e line 1, near "; }"
Execution of -e aborted due to compilation errors.

Could someone please point me to an explanation?

It's a subtle difference in Perl's grammar:

/* Loops: while, until, for, and a bare block */
loop : label WHILE '(' remember texpr ')' mintro mblock cont
{ ... }
| label UNTIL '(' remember iexpr ')' mintro mblock cont
{ ... }

Note the distinction into 'texpr' and 'iexpr', the difference being that
'texpr' is an expression where empty means true so 'while ()' is the
same as 'while (1)'.

I am quite sure this has been done on purpose because 'until () { ... }'
would then be the same as 'until (1) { ... }', meaning: the body of the
loop is never executed.

Still, it would be nice to turn 'until ()' into 'until (0)' so a
bug-report (see 'perldoc perlbug') might still be in order here.

Tassilo
 
J

John Nurick

It's a subtle difference in Perl's grammar:

/* Loops: while, until, for, and a bare block */
loop : label WHILE '(' remember texpr ')' mintro mblock cont
{ ... }
| label UNTIL '(' remember iexpr ')' mintro mblock cont
{ ... }

Note the distinction into 'texpr' and 'iexpr', the difference being that
'texpr' is an expression where empty means true so 'while ()' is the
same as 'while (1)'.

I am quite sure this has been done on purpose because 'until () { ... }'
would then be the same as 'until (1) { ... }', meaning: the body of the
loop is never executed.

Still, it would be nice to turn 'until ()' into 'until (0)' so a
bug-report (see 'perldoc perlbug') might still be in order here.

Thank you for the info. I've submitted a bug report ('wishlist'
priority). I like the way that turning 'until ()' into 'until (0)'
would mean that 'until ()' and 'while ()' worked the same.

John
 
J

John Nurick

Which begs the inevitable question: why not just use
'while ()'? Just for a bit more syntactic sugar?

Not that it wouldn't be nice, if only for symmetry's sake.
But can you think of any practical use?

No - but I'm not a Perl hacker<g>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top