summaryrefslogtreecommitdiff
path: root/t
diff options
context:
space:
mode:
authorNicholas Clark <nick@ccl4.org>2021-09-15 18:43:22 +0000
committerNicholas Clark <nick@ccl4.org>2021-09-16 13:54:42 +0000
commit47e2d53c92ba2eab83a1cac8dc3c3ed56a7cfe5e (patch)
tree43d289bcf71cfedf6791e4e899e65a2aaaedb8da /t
parent1a40fa6ddbc0f611355e78ab3e623e38d54977c6 (diff)
downloadperl-47e2d53c92ba2eab83a1cac8dc3c3ed56a7cfe5e.tar.gz
Test that for's iterator aliases the iterated list
Diffstat (limited to 't')
-rw-r--r--t/op/for.t34
1 files changed, 34 insertions, 0 deletions
diff --git a/t/op/for.t b/t/op/for.t
index d199cc8a82..ebe62b93ac 100644
--- a/t/op/for.t
+++ b/t/op/for.t
@@ -685,4 +685,38 @@ is(fscope(), 1, 'return via loop in sub');
is "@b", "bar", " RT #133558 reverse list";
}
+{
+ my @numbers = 0..2;
+ for my $i (@numbers) {
+ ++$i;
+ }
+ is("@numbers", '1 2 3', 'for iterators are aliases');
+
+ my @letters = qw(a b c);
+
+ for my $i (@numbers, @letters) {
+ ++$i;
+ }
+ is("@numbers", '2 3 4', 'iterate on two arrays together one');
+ is("@letters", 'b c d', 'iterate on two arrays together two');
+
+ my $got = eval {
+ for my $i (@letters, undef, @numbers) {
+ ++$i;
+ }
+ 1;
+ };
+ is($got, undef, 'aliased rvalue');
+ like($@, qr/^Modification of a read-only value attempted/,
+ 'aliased rvalue threw the correct exception');
+
+ is("@letters", 'c d e', 'letters were incremented');
+ is("@numbers", '2 3 4', 'numbers were not');
+
+ for my $i (@numbers[0, 1, 0]) {
+ ++$i;
+ }
+ is("@numbers", '4 4 4', 'array slices are lvalues');
+}
+
done_testing();