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

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

Ronald J Kimball
April 1, 2003 08:47
Re: [perl #21785] The magic of while (Patch 900) never got documented
Message ID:
On Tue, Apr 01, 2003 at 01:33:10PM -0000, (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)) {}


while (each %hash) {}

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

Ronald Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About