Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

It's a bug (was: When does while() test for defined vs truth)

by robin (Chaplain)
on Dec 17, 2001 at 20:23 UTC ( #132548=note: print w/replies, xml ) Need Help??

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:

  • glob(...)
  • $variable = <FILEHANDLE>
  • $variable = glob(...)

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;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://132548]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2017-02-23 03:39 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (338 votes). Check out past polls.