This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
scalar(%h = (1,1,1,1)) should return 4, not 2
authorRuslan Zakirov <ruz@bestpractical.com>
Sun, 7 Oct 2012 22:30:54 +0000 (02:30 +0400)
committerFather Chrysostomos <sprout@cpan.org>
Tue, 11 Dec 2012 16:59:39 +0000 (08:59 -0800)
perldoc perlop says:

    a list assignment in scalar context returns the number of elements
    produced by the expression on the right hand side of the assignment

Behaviour was changed as side effect of
ca65944e8ff8fff6e36ea7476ba807be16cfe2a9 where goal was to fix
return value in list context.

ext/Hash-Util-FieldHash/t/11_hashassign.t
pp_hot.c
t/op/hashassign.t

index e492fa2..d3e45d3 100644 (file)
@@ -282,9 +282,9 @@ foreach my $chr (60, 200, 600, 6000, 60000) {
     fieldhash %h;
     is( (join ':', %h = (1) x 8), '1:1',
        'hash assignment in list context removes duplicates' );
-    is( scalar( %h = (1,2,1,3,1,4,1,5) ), 2,
+    is( scalar( %h = (1,2,1,3,1,4,1,5) ), 8,
        'hash assignment in scalar context' );
-    is( scalar( ($x,%h) = (0,1,2,1,3,1,4,1,5) ), 3,
+    is( scalar( ($x,%h) = (0,1,2,1,3,1,4,1,5) ), 9,
        'scalar + hash assignment in scalar context' );
     $ar = [ %h = (1,2,1,3,1,4,1,5) ];
     is( $#$ar, 1, 'hash assignment in list context' );
index feba395..510d62b 100644 (file)
--- a/pp_hot.c
+++ b/pp_hot.c
@@ -1234,7 +1234,7 @@ PP(pp_aassign)
     else if (gimme == G_SCALAR) {
        dTARGET;
        SP = firstrelem;
-       SETi(lastrelem - firstrelem + 1 - duplicates);
+       SETi(lastrelem - firstrelem + 1);
     }
     else {
        if (ary)
index 37a7674..906e7ca 100644 (file)
@@ -280,9 +280,9 @@ foreach my $chr (60, 200, 600, 6000, 60000) {
        'hash assignment in list context removes duplicates' );
     is( (join ':', %h = qw(a 1 a 2 b 3 c 4 d 5 d 6)), 'a:2:b:3:c:4:d:6',
        'hash assignment in list context removes duplicates 2' );
-    is( scalar( %h = (1,2,1,3,1,4,1,5) ), 2,
+    is( scalar( %h = (1,2,1,3,1,4,1,5) ), 8,
        'hash assignment in scalar context' );
-    is( scalar( ($x,%h) = (0,1,2,1,3,1,4,1,5) ), 3,
+    is( scalar( ($x,%h) = (0,1,2,1,3,1,4,1,5) ), 9,
        'scalar + hash assignment in scalar context' );
     $ar = [ %h = (1,2,1,3,1,4,1,5) ];
     is( $#$ar, 1, 'hash assignment in list context' );