This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
add lvalue scalar alias tests to lvref.t
authorDavid Mitchell <davem@iabyn.com>
Sun, 5 Nov 2023 18:43:45 +0000 (18:43 +0000)
committerDavid Mitchell <davem@iabyn.com>
Mon, 4 Dec 2023 16:32:42 +0000 (16:32 +0000)
This is a compile-time error:

   (\$x = \1) = \2;

However, the following is legal code, but is untested (the 'OPf_MOD'
branch in pp_refassign() is not triggered by the test suite):

   f(\$x = \1);

So this commit adds some tests. It assumes that the above code is
equivalent to:

   \$x = \1;
   f(\$x);

Whether the existing behaviour is sane is up for debate, but if
if we change it, it should be a deliberate choice, not an accidental
change not spotted by the existing tests.

t/op/lvref.t

index 9a7dcb7..14362d5 100644 (file)
@@ -5,7 +5,7 @@ BEGIN {
     set_up_inc("../lib");
 }
 
-plan 198;
+plan 201;
 
 eval '\$x = \$y';
 like $@, qr/^Experimental aliasing via reference not enabled/,
@@ -86,6 +86,23 @@ for (1,2) {
   }
 }
 
+# Scalars in lvalue context
+
+{
+    my $s = 3;
+    my $t = 5;
+
+    sub foo1 {
+        ok ref($_[0]),   "foo1(alias) passes ref";
+        is ${$_[0]}, 5,  "foo1(alias) passes ref to t";
+        ${$_[0]} = 7;
+    }
+    foo1(\$s = \$t);
+    is $s, 7,  "foo1(alias) passes ref to t"
+}
+
+
+
 # Array Elements
 
 sub expect_scalar_cx { wantarray ? 0 : \$_ }