Skip Menu |
Report information
Id: 132644
Status: pending release
Priority: 0/
Queue: perl5

Owner: Nobody
Requestors: public [at] khwilliamson.com
Cc:
AdminCc:

Operating System: (no value)
PatchStatus: (no value)
Severity: low
Type: unknown
Perl Version: (no value)
Fixed In: 5.27.8



CC: dbook [...] cpan.org
To: perlbug [...] perl.org
Date: Fri, 22 Dec 2017 10:39:34 -0700
Subject: The 'each' function documentation is missing some information.
From: Karl Williamson <public [...] khwilliamson.com>
Download (untitled) / with headers
text/plain 789b
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.
RT-Send-CC: perl5-porters [...] perl.org
On Fri, 22 Dec 2017 17:39:52 GMT, public@khwilliamson.com wrote: Show quoted text
> 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)
From: Dan Book <grinnz [...] gmail.com>
Subject: Re: [perl #132644] The 'each' function documentation is missing some information.
To: Dave Mitchell via RT <perlbug-followup [...] perl.org>
Date: Fri, 22 Dec 2017 16:18:37 -0500
Download (untitled) / with headers
text/plain 1.6k
On Fri, Dec 22, 2017 at 4:04 PM, James E Keenan via RT <perlbug-followup@perl.org> wrote:
Show quoted text
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
From: Zefram <zefram [...] fysh.org>
Subject: Re: [perl #132644] The 'each' function documentation is missing some information.
To: perl5-porters [...] perl.org
Date: Sat, 23 Dec 2017 11:53:01 +0000
Download (untitled) / with headers
text/plain 113b
Fixed in commits 1a267a5111f1c4b1395ae3696820f177bf25b7c0 and 5e979393c70aea85c7e746cb74286755c4e720f4. -zefram


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org