You have not said what you are trying to do, or what behavior you expect, so all I can do is make some observations:
- The documentation for (??{ *code* }) begins: "WARNING: Using this feature safely requires that you understand its limitations." For me this would be a red flag strongly suggesting that whatever you are trying to do you should choose a different implementation.
- The aforesaid documentation says that whatever the code returns is treated as a fragment of your regular expression. Absent an explicit return, your code returns whatever its last statement (a print) returns. This is documented as a true value, which means pretty much anything but undef, 0, or '', and may well vary from execution to execution.
- Modifying any Perl variable resets its pos(), which is the regexp engine's notion of where it is in the string it is matching. I suspect it would take someone with deep knowledge of the regex engine to say what it will do if you modify pos() (or the variable itself) while it is being matched, and I further suspect that whatever it does may vary from release to release.
The usual way to modify a string is to use the substitution operator s/regexp/replacement/.