in reply to Recursively executed function in regex RHS, AKA "wait, why does this work?"

g in your regex means that it'll happen globally, so every instance will get processed, so line 7 actually gets fired multiple times. It APPEARS that since the line is getting modified after the current evaluation, that gets caught and processed. Testing seems to confirm this, as when you replace the IDs with 5 it works as expected. As to an actual reason as to WHY this is, I'd be interested in knowing myself. I assume that backtracking has something to do with this. perlre's section on backtracking may explain it better.

See Choroba's response. I missed the quite obvious fact that it was calling itself.
  • Comment on Re: Recursively executed function in regex RHS, AKA "wait, why does this work?"
  • Download Code

Replies are listed 'Best First'.
Re^2: Recursively executed function in regex RHS, AKA "wait, why does this work?"
by Eily (Monsignor) on Oct 21, 2014 at 13:18 UTC

    You're still right about /g being necessary for this to work. The recursion processes the descendants and the /g modifier makes sure all siblings are processed. There are actually two loop-like constructs in this code. s/REG/REPL/g does not however process the replaced text, or something like s/(.)/$1$1/g; would be an infinite loop.