Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Strange behaviour when regex variable is modified during match?

by ikegami (Patriarch)
on Jan 18, 2021 at 15:57 UTC ( [id://11127060]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11127060]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-19 05:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found