in reply to Re: lvalue substring oddities
in thread lvalue substring oddities

To reply to your sub :lvalue case, the index shouldn't be kept from one call to prefix() to the next with either the existing or proposed behaviour. However there is a bug (#24200) with substr used in an :lvalue sub. That bug should be fixed whatever else happens. (The bug is that rvalue calls to the sub will not work correctly after the first lvalue call.)

Aside from that, your code is a perfect example of how things can go wrong the way things are. Just saying (prefix = "AB") = "ABC" gets rid of the first '-'; with the proposed change it would leave $foo back in its original state.

Update to respond to Zaxo's update; the "surprise" is exactly the subject of the thread. When you say $foo = substr() = $bar substr returns a magic value that is first stored and then fetched. The "problem" is that both use exactly the same length, even if the store expanded or contracted the string. substr() being replaced by an :lvalue sub is exactly the same case...the sub is called only once but the magic value returned is acted on twice, both times with the originally determined length.