From: Tassilo von Parseval Date: Wed, 20 Jul 2005 10:43:11 +0000 (+0200) Subject: lvalue-subs returning elements of tied hashes/arrays X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f9bc45eff51a0e2fac1537ecee1124be910c832e;p=p5sagit%2Fp5-mst-13.2.git lvalue-subs returning elements of tied hashes/arrays Message-id: <20050720084311.GA20332@ethan> p4raw-id: //depot/perl@25194 --- diff --git a/pp_hot.c b/pp_hot.c index 1e3d5ea..8298026 100644 --- a/pp_hot.c +++ b/pp_hot.c @@ -2441,7 +2441,10 @@ PP(pp_leavesublv) MARK = newsp + 1; EXTEND_MORTAL(1); if (MARK == SP) { - if (SvFLAGS(TOPs) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) { + /* Temporaries are bad unless they happen to be elements + * of a tied hash or array */ + if (SvFLAGS(TOPs) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY) && + !(SvRMAGICAL(TOPs) && mg_find(TOPs, PERL_MAGIC_tiedelem))) { LEAVE; cxstack_ix--; POPSUB(cx,sv); diff --git a/t/op/sub_lval.t b/t/op/sub_lval.t index c161b4b..953a1e0 100755 --- a/t/op/sub_lval.t +++ b/t/op/sub_lval.t @@ -1,4 +1,4 @@ -print "1..68\n"; +print "1..72\n"; BEGIN { chdir 't' if -d 't'; @@ -530,8 +530,51 @@ sub lval2 : lvalue { $ary[1]; } print "not " unless join(':', @ary) eq "1:2:6"; print "ok 64\n"; +# check that an element of a tied hash/array can be assigned to via lvalueness + +package Tie_Hash; + +our ($key, $val); +sub TIEHASH { bless \my $v => __PACKAGE__ } +sub STORE { ($key, $val) = @_[1,2] } + +package main; +sub lval_tie_hash : lvalue { + tie my %t => 'Tie_Hash'; + $t{key}; +} + +eval { lval_tie_hash() = "value"; }; + +print "# element of tied hash: $@\nnot " if $@; +print "ok 65\n"; + +print "not " if "$Tie_Hash::key-$Tie_Hash::val" ne "key-value"; +print "ok 66\n"; + + +package Tie_Array; + +our @val; +sub TIEARRAY { bless \my $v => __PACKAGE__ } +sub STORE { $val[ $_[1] ] = $_[2] } + +package main; +sub lval_tie_array : lvalue { + tie my @t => 'Tie_Array'; + $t[0]; +} + +eval { lval_tie_array() = "value"; }; + +print "# element of tied array: $@\nnot " if $@; +print "ok 67\n"; + +print "not " if $Tie_Array::val[0] ne "value"; +print "ok 68\n"; + require './test.pl'; -curr_test(65); +curr_test(69); TODO: { local $TODO = 'test explicit return of lval expr';