develooper Front page | perl.perl5.porters | Postings from January 2001

[PATCH] lvalue hash and array elements

Thread Next
From:
Simon Cozens
Date:
January 2, 2001 15:16
Subject:
[PATCH] lvalue hash and array elements
Message ID:
20010102223300.A25411@deep-dark-truthful-mirror.perlhacker.org
Continuing the lvalue implementation blitz:

--- pp.h~	Tue Jan  2 19:56:39 2001
+++ pp.h	Tue Jan  2 20:04:17 2001
@@ -380,3 +380,13 @@
     SvREFCNT_dec(tmpRef);                   \
     SvRV(rv)=AMG_CALLun(rv,copy);        \
   } } STMT_END
+
+/*
+=for apidoc mU||LVRET
+True if the next operation will cause a return from an lvalue subroutine
+
+*/
+#define LVRET (PL_op->op_next && (PL_op->op_next->op_type == OP_LEAVESUBLV || \
+            (PL_op->op_next->op_type == OP_RETURN && ( \
+            (&cxstack[cxstack_ix])->blk_sub.lval && \
+            CvLVALUE((&cxstack[cxstack_ix])->blk_sub.cv)))))
--- pp_hot.c~	Tue Jan  2 19:46:30 2001
+++ pp_hot.c	Tue Jan  2 22:23:12 2001
@@ -505,7 +505,7 @@
 {
     djSP;
     AV *av = GvAV(cGVOP_gv);
-    U32 lval = PL_op->op_flags & OPf_MOD;
+    U32 lval = (PL_op->op_flags & OPf_MOD) || LVRET;
     SV** svp = av_fetch(av, PL_op->op_private, lval);
     SV *sv = (svp ? *svp : &PL_sv_undef);
     EXTEND(SP, 1);
@@ -1574,7 +1574,7 @@
     SV **svp;
     SV *keysv = POPs;
     HV *hv = (HV*)POPs;
-    U32 lval = PL_op->op_flags & OPf_MOD;
+    U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
     U32 defer = PL_op->op_private & OPpLVAL_DEFER;
     SV *sv;
     U32 hash = (SvFAKE(keysv) && SvREADONLY(keysv)) ? SvUVX(keysv) : 0;
@@ -2782,7 +2782,7 @@
     SV* elemsv = POPs;
     IV elem = SvIV(elemsv);
     AV* av = (AV*)POPs;
-    U32 lval = PL_op->op_flags & OPf_MOD;
+    U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
     U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
     SV *sv;
 
--- t/pragma/sub_lval.t~	Tue Jan  2 22:24:12 2001
+++ t/pragma/sub_lval.t	Tue Jan  2 22:28:53 2001
@@ -1,4 +1,4 @@
-print "1..50\n";
+print "1..54\n";
 
 BEGIN {
     chdir 't' if -d 't';
@@ -455,3 +455,25 @@
 $a = "not ok 50\n";
 baz() = "ok 50\n";
 print $a;
+
+{
+my %hash; my @array;
+sub alv : lvalue { $array[1] }
+sub alv2 : lvalue { $array[$_[0]] }
+sub hlv : lvalue { $hash{"foo"} }
+sub hlv2 : lvalue { $hash{$_[0]} }
+$array[1] = "not ok 51\n";
+alv() = "ok 51\n";
+print alv();
+
+alv2(20) = "ok 52\n";
+print $array[20];
+
+$hash{"foo"} = "not ok 53\n";
+hlv() = "ok 53\n";
+print $hash{foo};
+
+$hash{bar} = "not ok 54\n";
+hlv("bar") = "ok 54\n";
+print hlv("bar");
+}

-- 
Everything that can ever be invented has been invented 
    - Charles H. Duell, Commisioner of U.S. Patents, 1899.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About