in reply to Strange behaviour when regex variable is modified during match?
This is apparently a bug/limitation related to the copy-on-write (COW) mechanism.
In the first snippet, $A and the constant initially share the same string buffer. Once $A is modified, the COW mechanism comes into play making an unshared copy of the string buffer for $A. Something goes wrong at this point.
The workaround causes $A to have an unshared buffer going in.
Note that simply appending to a string can cause the string's buffer to be replaced with another larger one. This apparently doesn't trigger the bug. If you use Devel::Peek's Dump on the scalar before and after the m//, you'll see the address of the string buffer (PV) has changed. Even when the workaround is in play. So it seems to be a problem specific to the COW mechanism (added to Perl in version 5.20).
Finally, you shouldn't be surprised to have problems when modifying a variable on which an operation is being performed. for (@a) { say; pop(@a); } will give "interesting" results too, for example.
|
|---|