Skip to content

Commit

Permalink
Regression tests and documentation for n-at-a-time for.
Browse files Browse the repository at this point in the history
  • Loading branch information
nwc10 committed Jun 28, 2021
1 parent ff2aaa4 commit 4909d02
Show file tree
Hide file tree
Showing 3 changed files with 377 additions and 0 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Expand Up @@ -5833,6 +5833,7 @@ t/op/filetest_stack_ok.t See if file tests leave their argument on the stack
t/op/filetest_t.t See if -t file test works
t/op/flip.t See if range operator works
t/op/for.t See if for loops work
t/op/for-many.t See if n-at-a-time for loops work
t/op/fork.t See if fork works
t/op/fresh_perl_utf8.t UTF8 tests for pads and gvs
t/op/getpid.t See if $$ and getppid work with threads
Expand Down
27 changes: 27 additions & 0 deletions pod/perlsyn.pod
Expand Up @@ -282,6 +282,14 @@ The following compound statements may be used to control flow:

PHASE BLOCK

As of Perl 5.36, you can iterate over multiple values at a time by specifying
a list of lexicals within parentheses

LABEL for my (VAR, VAR) (LIST) BLOCK
LABEL for my (VAR, VAR) (LIST) BLOCK continue BLOCK
LABEL foreach my (VAR, VAR) (LIST) BLOCK
LABEL foreach my (VAR, VAR) (LIST) BLOCK continue BLOCK

If enabled by the experimental C<try> feature, the following may also be used

try BLOCK catch (VAR) BLOCK
Expand Down Expand Up @@ -549,6 +557,14 @@ followed by C<my>. To use this form, you must enable the C<refaliasing>
feature via C<use feature>. (See L<feature>. See also L<perlref/Assigning
to References>.)

As of Perl 5.36, you can iterate over a list of lexical scalars n-at-a-time.
If the size of the LIST is not an exact multiple of number of iterator
variables, then on the last iteration the "excess" iterator variables are
undefined values, much like if you slice beyond the end of an array. You
can only iterate over scalars - unlike list assignment, it's not possible to
use C<undef> to signify a value that isn't wanted. This is a limitation of
the current implementation, and might be changed in the future.

Examples:

for (@ary) { s/foo/bar/ }
Expand All @@ -574,6 +590,17 @@ Examples:
# do something which each %hash
}

foreach my ($foo, $bar, $baz) (@list) {
# do something three-at-a-time
}

foreach my ($key, $value) (%hash) {
# iterate over the hash
# The hash is eagerly flattened to a list before the loop starts,
# but as ever keys are copies, values are aliases.
# This is the same behaviour as for $var (%hash) {...}
}

Here's how a C programmer might code up a particular algorithm in Perl:

for (my $i = 0; $i < @ary1; $i++) {
Expand Down

0 comments on commit 4909d02

Please sign in to comment.