Where is "while ()" documented?

K

kj

I've looked in perlsyn, but I can't find documentation for the
special case in which the expression controlling the while loop is
empty, e.g.

while () {
# infinite loop
}

I.e. "while ()" apparently behaves like "while (1)". Is this
documented anywhere?

Thanks,

kj
 
P

Paul Lalli

kj said:
I've looked in perlsyn, but I can't find documentation for the
special case in which the expression controlling the while loop is
empty, e.g.

while () {
# infinite loop
}

I.e. "while ()" apparently behaves like "while (1)". Is this
documented anywhere?

There was a converstation about this "feature" over on the Perl Monks a
few months ago:
http://www.perlmonks.org/index.pl?node=464367&go_button=Search

Consensus seems to be that while () is defined to behave as while (1),
but that it's an undocumented feature. I, personally, would take that
to mean "don't do that."

Paul Lalli
 
J

John W. Krahn

kj said:
I've looked in perlsyn, but I can't find documentation for the
special case in which the expression controlling the while loop is
empty, e.g.

while () {
# infinite loop
}

I.e. "while ()" apparently behaves like "while (1)". Is this
documented anywhere?

I believe it's related to the fact that "for (;;) {...}" (which was inherited
from C) is an infinite loop with an empty conditional.


John
 
E

ed.overton

kj said:
I.e. "while ()" apparently behaves like "while (1)". Is this
documented anywhere?

A bit tangential, but I found it interesting that the structure makes a
difference in the behavior (at least, it does in 5.8.4). This is
infinite:

perl -wle "while() { print qq(hello) }"

This is not infinite:

perl -wle "print qq(hello) while()"

Ed
 
J

John W. Krahn

A bit tangential, but I found it interesting that the structure makes a
difference in the behavior (at least, it does in 5.8.4). This is
infinite:

perl -wle "while() { print qq(hello) }"

This is not infinite:

perl -wle "print qq(hello) while()"

That is because the second example is a statement modifier where the
parentheses are superfluous, unlike a while loop where the parentheses are
syntactically required.


John
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top