Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

comment on

( #3333=superdoc: print w/replies, xml ) 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:

"12180" =~ m{ (?{ local @n = () }) (?: (\d) (?{ local @n = (@n, $1) }) )+ \d (?{ @d = @n }) }x;
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:
local @n; /(.)(?{ ++$n[0] })^/; @d = @n;
First thing last: regex compilation is an interesting thing. Here is code that compiles the regex twice:
$p = '\w+-\d+'; /$p/; /$p/;
And here's code that only compiles it once:
$p = '\w+-\d+'; for $i (1,2) { /$p/ }
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:

($x,$y) = ('a+', 'b'); for (1,2) { /$x$y/; ($x,$y) = ('a', '+b'); }
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.

Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

In reply to Re: Re2: Perl Bug in Regex Code Block? by japhy
in thread Perl Bug in Regex Code Block? by Hofmator

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (4)
    As of 2019-05-25 13:32 GMT
    Find Nodes?
      Voting Booth?
      Do you enjoy 3D movies?

      Results (152 votes). Check out past polls.

      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!