|Just another Perl shrine|
Re: Re2: Perl Bug in Regex Code Block?by japhy (Canon)
|on Sep 03, 2001 at 20:13 UTC||Need Help??|
Last thing first: it's not documented because code evaluation is experimental. It's a very iffy thing, and it changes quickly and silently.
Second thing second: use a local array, and copy its contents to a lexical one. I know you don't want to use a global array, but I'm telling you that you should. This is an example from my book:
We make a local array that things happen to, and then we copy it to our real array at the end of the regex. In your case, you might want to do:
First thing last: regex compilation is an interesting thing. Here is code that compiles the regex twice:
And here's code that only compiles it once:
The secret is this (and pertains to regexes with variables in them, for they're not compiled until run-time): for each compilation op-code in the syntax tree, Perl keeps a string representation of the regex. The next time the compilation op-code is gotten to, the NEW string representation is compared with the previous one. If they are the same, the regex doesn't need recompilation. If they are different, it does need to be recompiled.
Now, if you've heard "if you have a regex, and it has variables in it, and the variables change, the regex has to be recompiled" that's technically incorrect:
The two variables comprising our regex have changed, but the regex ends up being the same. Sneaky, eh?
I can't take credit for figuring this out on my own -- a couple months ago, Dominus gave me the hint about the string representation. Now I understand.
So that answers your question, I think.