Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by robin (Chaplain)
on Dec 17, 2001 at 20:23 UTC ( [id://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:

  • <FILEHANDLE>
  • 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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://132548]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-03-19 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found