http://www.perlmonks.org?node_id=132548


in reply to When does while() test for defined vs truth

Update: Jarkko has now applied this patch, so it'll be fixed in the next version of perl.

As several other respondents have noted, certain constructs should always be treated as implicit definedness tests when used in the loop condition of a while loop. Those constructs are:

What blakem pointed out is that this isn't working consistently, and if you write a loop of the form 1 while $variable = glob('*.pl'), then the test isn't wrapped in a defined() call, as it should be.

It's a bug in perl. A typo in the newLOOPOP function of op.c, to be precise. It can be fixed by applying the following patch (against bleadperl). I'll send the patch to p5p, and it ought to go into 5.8. Well spotted, blakem!

--- op.c.orig Mon Dec 17 13:51:32 2001 +++ op.c Mon Dec 17 13:55:47 2001 @@ -4073,7 +4073,7 @@ case OP_SASSIGN: if (k1->op_type == OP_READDIR || k1->op_type == OP_GLOB - || (k1->op_type == OP_NULL && k1->op_targ == OP_ +NULL) + || (k1->op_type == OP_NULL && k1->op_targ == OP_ +GLOB) || k1->op_type == OP_EACH) expr = newUNOP(OP_DEFINED, 0, expr); break;