Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by Anonymous Monk
on Jan 17, 2021 at 19:08 UTC ( [id://11127041]=note: print w/replies, xml ) Need Help??


in reply to Strange behaviour when regex variable is modified during match?

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/.

Replies are listed 'Best First'.
Re^2: Strange behaviour when regex variable is modified during match?
by rsFalse (Chaplain) on Jan 17, 2021 at 20:39 UTC
    I was only playing with regex and eval inside. I tried to generate some substitution patterns, e.g. zipping or meshing chars.

    Sincerely, earlier today I found other "bug" (=feature) that $_ inside (?{ ... }) binds to LHS after I tried to manipulate $_ as a global. But I found in perlre that it is desired behaviour. So then I tried to play with this feature.
Re^2: Strange behaviour when regex variable is modified during match?
by Anonymous Monk on Jan 18, 2021 at 16:01 UTC
    Definitely you are playing too close to the underlying implementation of regex and need to find a different way to do what you're trying.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-25 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found