diff options
author | Father Chrysostomos <sprout@cpan.org> | 2011-12-04 10:47:40 -0800 |
---|---|---|
committer | Father Chrysostomos <sprout@cpan.org> | 2011-12-04 10:50:48 -0800 |
commit | 83f78d1a27d5727dabfc8bcc2b961cb405b831e9 (patch) | |
tree | 77674fd3efedbd14bec2f2ffa6176678ff28fa67 /proto.h | |
parent | 7ba26d48121ff365601a73eefc7798693a3a9118 (diff) | |
download | perl-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 'proto.h')
-rw-r--r-- | proto.h | 8 |
1 files changed, 8 insertions, 0 deletions
@@ -5601,6 +5601,14 @@ STATIC void S_save_magic(pTHX_ I32 mgs_ix, SV *sv) STATIC void S_unwind_handler_stack(pTHX_ const void *p); #endif +#if defined(PERL_IN_MG_C) || defined(PERL_IN_PP_C) +PERL_CALLCONV bool Perl_translate_substr_offsets(pTHX_ STRLEN curlen, IV pos1_iv, bool pos1_is_uv, IV len_iv, bool len_is_uv, STRLEN *posp, STRLEN *lenp) + __attribute__nonnull__(pTHX_6) + __attribute__nonnull__(pTHX_7); +#define PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS \ + assert(posp); assert(lenp) + +#endif #if defined(PERL_IN_MRO_C) STATIC void S_mro_clean_isarev(pTHX_ HV * const isa, const char * const name, const STRLEN len, HV * const exceptions, U32 flags) __attribute__nonnull__(pTHX_1) |