SPUG: interesting while() behavior and hosting recs

John W. Krahn krahnj at telus.net
Sun Oct 3 05:58:00 CDT 2004


Joel Grow wrote:
> 
> John pointed out:
> 
>  That is working for the same reason that
> 
>  for ( ;; ) { ... }
> 
>  works in Perl as well as in C where Perl inherited it from.
> 
> I guess that's a good enough explanation, but it still seems to me like
> 
>  while () {
>  }
> 
> should intuitively evaluate to false.  I'll get over it.  Or at least
> memorize it.
> 
> Btw, if you really want to use while() without the parens, you could do:
> 
>  do {
>     print "hello world\n";
>  } while $x++ < 3;
> 
> # not to my tastes, but tmtowtdi...

You could but it has two differences from "while (EXPR) {CODE BLOCK}".  The 
first is that the code block executes first before the expr is evaluated (just 
like in C) and second is that using do {} means that the keywords 'next', 
'last' and 'redo' won't work without adding another pair of braces to create a 
loop.  If you have a single statement like your example then you could just 
use 'print "hello world\n" while $x++ < 3;'



John
-- 
use Perl;
program
fulfillment


More information about the spug-list mailing list