summaryrefslogtreecommitdiff
path: root/sv.h
diff options
context:
space:
mode:
authorFather Chrysostomos <sprout@cpan.org>2011-12-04 10:47:40 -0800
committerFather Chrysostomos <sprout@cpan.org>2011-12-04 10:50:48 -0800
commit83f78d1a27d5727dabfc8bcc2b961cb405b831e9 (patch)
tree77674fd3efedbd14bec2f2ffa6176678ff28fa67 /sv.h
parent7ba26d48121ff365601a73eefc7798693a3a9118 (diff)
downloadperl-83f78d1a27d5727dabfc8bcc2b961cb405b831e9.tar.gz
Adjust substr offsets when using, not when creating, lvalue
When substr() occurs in potential lvalue context, the offsets are adjusted to the current string (negative being converted to positive, lengths reaching beyond the end of the string being shortened, etc.) as soon as the special lvalue to be returned is created. When that lvalue is assigned to, the original scalar is stringified once more. That implementation results in two bugs: 1) Fetch is called twice in a simple substr() assignment (except in void context, due to the special optimisation of commit 24fcb59fc). 2) These two calls are not equivalent: $SIG{__WARN__} = sub { warn "w ",shift}; sub myprint { print @_; $_[0] = 1 } print substr("", 2); myprint substr("", 2); The second one dies. The first one only warns. That’s mean. The error is also wrong, sometimes, if the original string is going to get longer before the substr lvalue is actually used. The behaviour of \substr($str, -1) if $str changes length is com- pletely undocumented. Before 5.10, it was documented as being unreli- able and subject to change. What this commit does is make the lvalue returned by substr remember the original arguments and only adjust the offsets when the assign- ment happens. This means that the following now prints z, instead of xyz (which is actually what I would expect): $str = "a"; $substr = \substr($str,-1); $str = "xyz"; print $substr;
Diffstat (limited to 'sv.h')
-rw-r--r--sv.h2
1 files changed, 2 insertions, 0 deletions
diff --git a/sv.h b/sv.h
index 33a61be109..42821444b4 100644
--- a/sv.h
+++ b/sv.h
@@ -482,6 +482,7 @@ struct xpvlv {
SV* xlv_targ;
char xlv_type; /* k=keys .=pos x=substr v=vec /=join/re
* y=alem/helem/iter t=tie T=tied HE */
+ char xlv_flags; /* 1 = negative offset 2 = negative len */
};
/* This structure works in 3 ways - regular scalar, GV with GP, or fast
@@ -1325,6 +1326,7 @@ the scalar's value cannot change unless written to.
#define LvTARG(sv) ((XPVLV*) SvANY(sv))->xlv_targ
#define LvTARGOFF(sv) ((XPVLV*) SvANY(sv))->xlv_targoff
#define LvTARGLEN(sv) ((XPVLV*) SvANY(sv))->xlv_targlen
+#define LvFLAGS(sv) ((XPVLV*) SvANY(sv))->xlv_flags
#define IoIFP(sv) (sv)->sv_u.svu_fp
#define IoOFP(sv) ((XPVIO*) SvANY(sv))->xio_ofp