diff options
author | Father Chrysostomos <sprout@cpan.org> | 2012-05-11 20:13:01 -0700 |
---|---|---|
committer | Father Chrysostomos <sprout@cpan.org> | 2012-05-21 21:40:04 -0700 |
commit | 8ae39f603f0f5778c160e18e08df60affbd5a620 (patch) | |
tree | a3591b4a2d8cd6aa0ab232cea2e43d6bcf9dd647 /t | |
parent | 552f3107b6148fdfe4d96e95d22a1c01e64d921e (diff) | |
download | perl-8ae39f603f0f5778c160e18e08df60affbd5a620.tar.gz |
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! :-)
Diffstat (limited to 't')
-rw-r--r-- | t/op/defins.t | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/t/op/defins.t b/t/op/defins.t index 80127b444d..5b26bf8053 100644 --- a/t/op/defins.t +++ b/t/op/defins.t @@ -10,7 +10,7 @@ BEGIN { $SIG{__WARN__} = sub { $warns++; warn $_[0] }; } require 'test.pl'; -plan( tests => 19 ); +plan( tests => 23 ); my $unix_mode = 1; @@ -132,6 +132,7 @@ unlink($saved_filename); ok(!(-f $saved_filename),'work file unlinked'); my %hash = (0 => 1, 1 => 2); +my @array = 1; $seen = 0; while (my $name = each %hash) @@ -155,4 +156,30 @@ while ($where{$seen} = each %hash) } cmp_ok($seen,'==',1,'seen in each hash'); +$seen = 0; +undef $_; +while (each %hash) + { + $seen++ if $_ eq '0'; + } +cmp_ok($seen,'==',1,'0 seen in $_ in while(each %hash)'); + +$seen = 0; +undef $_; +while (each @array) + { + $seen++ if $_ eq '0'; + } +cmp_ok($seen,'==',1,'0 seen in $_ in while(each @array)'); + +$seen = 0; +undef $_; +$_ eq '0' and $seen++ while each %hash; +cmp_ok($seen,'==',1,'0 seen in $_ in while(each %hash) as stm mod'); + +$seen = 0; +undef $_; +$_ eq '0' and $seen++ while each @array; +cmp_ok($seen,'==',1,'0 seen in $_ in while(each @array) as stm mod'); + cmp_ok($warns,'==',0,'no warns at finish'); |