in reply to Re: Multiple uses of (?{ code }) do not appear to be called
in thread Multiple uses of (?{ code }) do not appear to be called

Why does that code block (as rhesa puts it) "get closed over?"

Because the current code implementing both (?{}) and (??{}) is a hack. The code is compiled once for performance reasons. Unfortunately its not simple to make it not a closure without a performance penalty. The issue you have think about is that the code could execute in almost any context due to qr//, thus the variable binding needs to occur at match start and needs to handle the case that there are no variables with the appropriate names to bind to, etc, etc.

You can see other warts in the implementation by doing certain forms of syntax error in the code, the error message will be distinctly unhelpful, and again apparently its a real bitch to fix.

---
$world=~s/war/peace/g

  • Comment on Re^2: Multiple uses of (?{ code }) do not appear to be called

Replies are listed 'Best First'.
Re^3: Multiple uses of (?{ code }) do not appear to be called
by TimToady (Parson) on Dec 30, 2006 at 17:21 UTC
    The only way to fix this is to take the Perl 6 approach and clean up the compilation semantics of regexes. All of these hacks are a direct result of trying to treat regexes as strings rather than as a real minilanguage. Interpolating variables into regexes prior to compilation is simply wrong. It destroys any semblance of lexical scoping for both variable bindings and error message location. If I were going to fix this in Perl 5, I'd make a lexically scoped pragma to compile regexes immediately with sane variable bindings to avoid all this two-pass compilation bogosity. Any other approach is just bandaids.

      Maybe I'm not seeing this properly, do you mean the issue of /$foo/ where $foo is a string? If so, its not clear to me how changing that makes the issue of rebinding compiled code into the pad of its usage context any easier.

      I can see how changing how variable interpolation is handled in regexes would make for a lot more flexibility in other respects, but its not clear how it helps the immediate issue of this thread... Can you explain a bit more please?

      ---
      $world=~s/war/peace/g