Make while(each ...) imply defined($_ = ...)
authorFather Chrysostomos <sprout@cpan.org>
Sat, 12 May 2012 03:13:01 +0000 (20:13 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Tue, 22 May 2012 04:40:04 +0000 (21:40 -0700)
commit8ae39f603f0f5778c160e18e08df60affbd5a620
treea3591b4a2d8cd6aa0ab232cea2e43d6bcf9dd647
parent552f3107b6148fdfe4d96e95d22a1c01e64d921e
Make while(each ...) imply defined($_ = ...)

This came up in ticket #108286.

Quoting Nicholas Clark:
>
>     while (<STDIN>)
>     while (<*>)
>
> These both always implicitly assigned to $_, always implicitly
> added defined.
>
>     while ($_ = <STDIN>)
>     while ($a = <STDIN>)
>     while ($_ = <*>)
>     while ($a = <*>)
>     while ($_ = readdir D)
>     while ($a = readdir D)
>     while ($_ = each %h)
>     while ($a = each %h)
>
> The implicit defined added was by commit 4b161ae29769b4a3,
> //depot/maint-5.004/perl@949
>
>
> BUT:
>
>     while (readdir D)
>
> The implicit assignment to $_ and defined test were both added in
> *2009* (by commit 114c60ecb1f7)
>
>
> leaving:
>
>     while (each %h)
>
>
> So it is the odd one out. And in 2009 we felt comfortable to add
> both the implicit assignment and the defined test in blead for
> readdir, as a bug fix, and have had no reports of it caus-
> ing problems.

[He asked:]
> > > So that's a bug?

[And I responded:]
> > That's what I was trying to ask. :-)
>
> OK, after a quite a bit of deliberation and digging, I'm of the opinion that
>
> 1: yes, it's a bug

...

> So, there's only one use of while(each %...) on CPAN outside of
> debugging or test code, and that's only go the potential to break
> due to assignment now happening to to $_. Compared with 29 matches
> for while\s*\(\s*readdir of which 4 are .pm files. So
>
> 2: I think it's safe to fix it, just like readdir was fixed.

Just *as* readdir was fixed! :-)
op.c
t/op/defins.t