RT #132644: The 'each' function documentation is missing some information.
-
Basics
- Id
- 132644
- Status
- resolved
-
Dates
- Created:
- 2017-12-22 17:39:52
- Author:
- Karl Williamson
- Last Updated:
- 2018-06-23 15:32:49
- Closed:
-
Custom Fields
- Fixed In:
- 5.27.8
- Operating System:
- PatchStatus:
- Perl Version:
- Severity:
- low
- Type:
- unknown
-
Links
- DependedOnBy:
- DependsOn:
- MemberOf:
- Members:
- ReferredToBy:
- RefersTo:
-
People
- Owner:
- Nobody in particular <>
- Requestors:
- karl williamson <publ...@khw...>
- Cc:
- AdminCC:
# karl williamson <publ...@khw...>
Fri, 22 Dec 2017 09:39:52 -0800
I am posting this for Dan Book, who is having trouble getting email to
perlbug through.
The build options and version of perl is immaterial here.
1. When called in scalar context in a while loop, the condition is wrapped
implicitly in a 'defined' check, much like with 'readline'.
e.g. `while (my $key = each %hash) { ... }` becomes
`while (defined(my $key = each %hash)) { ... }`, and
`while (each %hash) { ... }` becomes `while (defined($_ = each %hash)) {
... }`
(on 5.18+)
2. The iterator is not only shared and reset by calling 'keys' or
'values', but
also by other calls to 'each' on the same hash or array, and by
accessing the
hash in list context (such as for assignment to another structure, or
passing
to a function) - this last one only applies to hashes, not arrays.
# James E Keenan <jkee...@cpa...>
Fri, 22 Dec 2017 13:04:09 -0800
On Fri, 22 Dec 2017 17:39:52 GMT, [email protected] wrote:
> I am posting this for Dan Book, who is having trouble getting email to
> perlbug through.
>
> The build options and version of perl is immaterial here.
>
> 1. When called in scalar context in a while loop, the condition is wrapped
> implicitly in a 'defined' check, much like with 'readline'.
>
> e.g. `while (my $key = each %hash) { ... }` becomes
> `while (defined(my $key = each %hash)) { ... }`, and
> `while (each %hash) { ... }` becomes `while (defined($_ = each %hash)) {
> ... }`
> (on 5.18+)
>
> 2. The iterator is not only shared and reset by calling 'keys' or
> 'values', but
> also by other calls to 'each' on the same hash or array, and by
> accessing the
> hash in list context (such as for assignment to another structure, or
> passing
> to a function) - this last one only applies to hashes, not arrays.
Can you provide examples where failing to understand these concepts would trip someone up?
--
James E Keenan ([email protected])
# The RT System itself <>
Fri, 22 Dec 2017 13:04:10 -0800
# Dan Book <grin...@gma...>
Fri, 22 Dec 2017 13:18:53 -0800
On Fri, Dec 22, 2017 at 4:04 PM, James E Keenan via RT <
[email protected]> wrote:
> On Fri, 22 Dec 2017 17:39:52 GMT, [email protected] wrote:
> > I am posting this for Dan Book, who is having trouble getting email to
> > perlbug through.
> >
> > The build options and version of perl is immaterial here.
> >
> > 1. When called in scalar context in a while loop, the condition is
> wrapped
> > implicitly in a 'defined' check, much like with 'readline'.
> >
> > e.g. `while (my $key = each %hash) { ... }` becomes
> > `while (defined(my $key = each %hash)) { ... }`, and
> > `while (each %hash) { ... }` becomes `while (defined($_ = each %hash)) {
> > ... }`
> > (on 5.18+)
> >
> > 2. The iterator is not only shared and reset by calling 'keys' or
> > 'values', but
> > also by other calls to 'each' on the same hash or array, and by
> > accessing the
> > hash in list context (such as for assignment to another structure, or
> > passing
> > to a function) - this last one only applies to hashes, not arrays.
>
> Can you provide examples where failing to understand these concepts would
> trip someone up?
For 1, someone might assume that a `while (my $key = each %hash)` loop will
terminate if it encounters an empty string or '0' key, or be confused when
it doesn't, since there's no documentation similar to that for `while (my
$line = <$fh>)` which shares the behavior.
For 2, it would not be apparent that the uses I listed would be destructive
to such an iteration. Such as:
while (my ($k, $v) = each %hash) {
my %copy = %hash;
}
while (my ($k, $v) = each %hash) {
while (my ($inner_k, $inner_v) = each %hash) {
...
}
}
-Dan
# Zefram <zefr...@fys...>
Sat, 23 Dec 2017 03:53:17 -0800
Fixed in commits 1a267a5111f1c4b1395ae3696820f177bf25b7c0 and
5e979393c70aea85c7e746cb74286755c4e720f4.
-zefram
# Dave Mitchell <dave...@iab...>
Thu, 28 Dec 2017 03:14:57 -0800
# Karl Williamson <...@cpa...>
Sat, 23 Jun 2018 08:32:49 -0700
Thank you for filing this report. You have helped make Perl better.
With the release yesterday of Perl 5.28.0, this and 185 other issues have been
resolved.
Perl 5.28.0 may be downloaded via:
https://metacpan.org/release/XSAWYERX/perl-5.28.0
If you find that the problem persists, feel free to reopen this ticket.
# Karl Williamson <...@cpa...>
Sat, 23 Jun 2018 08:32:49 -0700