Re: localising hash element by variable
authorBo Lindbergh <blgl@hagernas.com>
Mon, 28 May 2007 20:26:00 +0000 (22:26 +0200)
committerRafael Garcia-Suarez <rgarciasuarez@gmail.com>
Tue, 29 May 2007 08:41:09 +0000 (08:41 +0000)
Message-Id: <BC2C451F-B286-4D38-923E-E3B473F7B5E1@hagernas.com>

p4raw-id: //depot/perl@31301

scope.c
t/op/local.t

diff --git a/scope.c b/scope.c
index 4b68f1b..964a3b9 100644 (file)
--- a/scope.c
+++ b/scope.c
@@ -545,7 +545,7 @@ Perl_save_helem(pTHX_ HV *hv, SV *key, SV **sptr)
     SvGETMAGIC(*sptr);
     SSCHECK(4);
     SSPUSHPTR(SvREFCNT_inc_simple(hv));
-    SSPUSHPTR(SvREFCNT_inc_simple(key));
+    SSPUSHPTR(newSVsv(key));
     SSPUSHPTR(SvREFCNT_inc(*sptr));
     SSPUSHINT(SAVEt_HELEM);
     save_scalar_at(sptr);
index 5aea967..489f409 100755 (executable)
@@ -5,7 +5,7 @@ BEGIN {
     @INC = qw(. ../lib);
     require './test.pl';
 }
-plan tests => 114;
+plan tests => 117;
 
 my $list_assignment_supported = 1;
 
@@ -428,3 +428,18 @@ sub f { ok(0 == $[); }
     
 }
 
+# when localising a hash element, the key should be copied, not referenced
+
+{
+    my %h=('k1' => 111);
+    my $k='k1';
+    {
+       local $h{$k}=222;
+
+       is($h{'k1'},222);
+       $k='k2';
+    }
+    ok(! exists($h{'k2'}));
+    is($h{'k1'},111);
+}
+