Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The 'each' function documentation is missing some information. #16334

Closed
p5pRT opened this issue Dec 22, 2017 · 8 comments
Closed

The 'each' function documentation is missing some information. #16334

p5pRT opened this issue Dec 22, 2017 · 8 comments
Labels

Comments

@p5pRT
Copy link
Collaborator

@p5pRT p5pRT commented Dec 22, 2017

Migrated from rt.perl.org#132644 (status was 'resolved')

Searchable as RT132644$

@p5pRT
Copy link
Collaborator Author

@p5pRT p5pRT commented Dec 22, 2017

From @khwilliamson

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.

@p5pRT
Copy link
Collaborator Author

@p5pRT p5pRT commented Dec 22, 2017

From @jkeenan

On Fri, 22 Dec 2017 17​:39​:52 GMT, public@​khwilliamson.com 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 (jkeenan@​cpan.org)

@p5pRT
Copy link
Collaborator Author

@p5pRT p5pRT commented Dec 22, 2017

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Collaborator Author

@p5pRT p5pRT commented Dec 22, 2017

From @Grinnz

On Fri, Dec 22, 2017 at 4​:04 PM, James E Keenan via RT <
perlbug-followup@​perl.org> wrote​:

On Fri, 22 Dec 2017 17​:39​:52 GMT, public@​khwilliamson.com 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

@p5pRT
Copy link
Collaborator Author

@p5pRT p5pRT commented Dec 23, 2017

From zefram@fysh.org

Fixed in commits 1a267a5 and
5e97939.

-zefram

@p5pRT
Copy link
Collaborator Author

@p5pRT p5pRT commented Dec 28, 2017

@iabyn - Status changed from 'open' to 'pending release'

@p5pRT
Copy link
Collaborator Author

@p5pRT p5pRT commented Jun 23, 2018

From @khwilliamson

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.

@p5pRT
Copy link
Collaborator Author

@p5pRT p5pRT commented Jun 23, 2018

@khwilliamson - Status changed from 'pending release' to 'resolved'

@p5pRT p5pRT closed this Jun 23, 2018
@p5pRT p5pRT added the Severity Low label Oct 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.