develooper Front page | perl.perl5.porters | Postings from April 2003

Re: [perl #21785] The magic of while (Patch 900) never got documented

From:
Ronald J Kimball
Date:
April 1, 2003 08:47
Subject:
Re: [perl #21785] The magic of while (Patch 900) never got documented
Message ID:
20030401164720.GB1069622@linguist.thayer.dartmouth.edu
On Tue, Apr 01, 2003 at 01:33:10PM -0000, kriegjcb@sunu991.rz.ruhr-uni-bochum.de (via RT) wrote:

> As perlmonks and the fifth German Perl workshop found out,
> "while" has some special magic in handling a few operators.
> 
> While (sic) everyone knows that
>   while(<>) {}
> 
> really means
> 
>   while(defined($_=<>)) {}
> 
> it never got documented that patch 900
> also introduced similar behaviou?r for
> "readdir", "glob" and "each".
> 
> One problem is where to document it (probably under "while"),
> but the old thing is documented under "readline".

This also holds in the conditional of a for loop.

for (;<>;) {}

really means

for (;defined($_=<>);) {}


> Another small problem for me is how "each" can ever return something defined
> but false.

In a scalar context, each just returns the key, so this can happen if 0 is
a key of the hash.

However, for each I don't think the assignment to $_ is implicit, only the
call to defined.  i.e.

while ($_ = each %hash) {}

really means

while (defined($_ = each %hash)) {}

but

while (each %hash) {}

doesn't assign to $_ and doesn't call defined.


Ronald



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About