C
Clyde Ingram
Odd one this:an empty EXPRession in a "while" loop seems to take a defined
value in this call from either ksh on solaris 2.6 to Perl 5.004, or Cygwin
on Win2k or Win98 to Perl 5.8.0 (ActiveState binary 806):
perl -e 'while () { print "Aha "; }'
because it loops endlessly, printing "Aha Aha Aha Aha . . .".
Same behaviour if I beef this up into:
perl -wTe 'use strict; while () { print "Aha "; }'
or even it I put this into a file (fred), make it executable (chmod 755
fred), and run it (perl fred).
Contrast this with:
perl -e 'do { print "Aha "; } while ();'
which only prints "Aha " once, and then stops.
Contrast still further with:
perl -e 'if () { print "Aha "; }'
which fails with:
syntax error at -e line 1, near "() "
Execution of -e aborted due to compilation errors.
Note also, these print nothing, as expected:
perl -e 'while (undef) { print "Aha "; }'
perl -e 'while ("") { print "Aha "; }'
perl -e 'while (0) { print "Aha "; }'
perl -e 'while (defined) { print "Aha "; }'
Now the Perl manual says:
if (EXPR) BLOCK
. . .
LABEL while (EXPR) BLOCK
. . .
The while statement executes the block as long as the expression is
true (does not evaluate to the null string "" or 0 or "0").
Can anyone explain why my very first "while ()" looped endlessly, but the
"do . . . while ()" stopped?
And should I be surprised that the "if ()" gave a syntax error instead?
Thank-you,
Clyde
value in this call from either ksh on solaris 2.6 to Perl 5.004, or Cygwin
on Win2k or Win98 to Perl 5.8.0 (ActiveState binary 806):
perl -e 'while () { print "Aha "; }'
because it loops endlessly, printing "Aha Aha Aha Aha . . .".
Same behaviour if I beef this up into:
perl -wTe 'use strict; while () { print "Aha "; }'
or even it I put this into a file (fred), make it executable (chmod 755
fred), and run it (perl fred).
Contrast this with:
perl -e 'do { print "Aha "; } while ();'
which only prints "Aha " once, and then stops.
Contrast still further with:
perl -e 'if () { print "Aha "; }'
which fails with:
syntax error at -e line 1, near "() "
Execution of -e aborted due to compilation errors.
Note also, these print nothing, as expected:
perl -e 'while (undef) { print "Aha "; }'
perl -e 'while ("") { print "Aha "; }'
perl -e 'while (0) { print "Aha "; }'
perl -e 'while (defined) { print "Aha "; }'
Now the Perl manual says:
if (EXPR) BLOCK
. . .
LABEL while (EXPR) BLOCK
. . .
The while statement executes the block as long as the expression is
true (does not evaluate to the null string "" or 0 or "0").
Can anyone explain why my very first "while ()" looped endlessly, but the
"do . . . while ()" stopped?
And should I be surprised that the "if ()" gave a syntax error instead?
Thank-you,
Clyde