summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Clark <nick@ccl4.org>2021-10-18 13:16:40 +0000
committerNicholas Clark <nick@ccl4.org>2021-10-18 14:10:14 +0000
commit65b7cc064468ea58fd900e8293dfb0fdd1de8092 (patch)
treedb55e6f20c0811591807c920d072635a092d9f5d
parent7b1885ae53fad4123864416d9ef0bbdb78a1882f (diff)
downloadperl-65b7cc064468ea58fd900e8293dfb0fdd1de8092.tar.gz
for CORE::my $var (...) {} is legal syntax, hence test it
We had tests for CORE::state, but not CORE::my or CORE::our. It happens that they are all legal syntax, but seemingly more be accident than design. They are only accepted by yyl_foreach() as a quirk of the current implementation. Note that for my Dog $spot (...) {} is legal, but not CORE::my. our Dog is legal, CORE::our is not. Neither state Dog nor CORE::state are legal. These are all emergent behaviour of the parser - do not take these tests as "correct", merely as verifying that current behaviour doesn't change unless we intended it to.
-rw-r--r--t/op/for.t37
1 files changed, 37 insertions, 0 deletions
diff --git a/t/op/for.t b/t/op/for.t
index ebe62b93ac..a4d80664d8 100644
--- a/t/op/for.t
+++ b/t/op/for.t
@@ -719,4 +719,41 @@ is(fscope(), 1, 'return via loop in sub');
is("@numbers", '4 4 4', 'array slices are lvalues');
}
+# It turns out that these are legal. Whether they should be is another matter.
+
+{
+ my @opinion = qw(Emergent Behaviour);
+ my @observation;
+ for CORE::my $var (@opinion) {
+ push @observation, $var;
+ }
+ is("@observation", "@opinion", 'for CORE::my $oh_my_oh_my ...');
+
+ @observation = ();
+ for CORE::our $var (@opinion) {
+ push @observation, $var;
+ }
+ is("@observation", "@opinion", 'for CORE::our $var ...');
+
+}
+
+# Likewise, we have this inconsistency currently:
+{
+ use feature "state";
+ ++$Dog::VERSION;
+
+ for my $token (qw(my our)) {
+ my $code = "for $token Dog \$spot ('Woof') { } 42";
+ is(eval $code, 42, "$code is valid");
+ is($@, "", "$code had no errors");
+ }
+
+ # But these are all invalid:
+ for my $token (qw(CORE::my CORE::our CORE::state state)) {
+ my $code = "for $token Dog \$spot ('Woof') { } 42 ";
+ is(eval $code, undef, "$code is not valid");
+ like($@, qr/^Missing \$ on loop variable/, "$code had emergent error");
+ }
+}
+
done_testing();