Jeffrey Friedl talks a bit about this in Mastering Regular Expressions in the section called "A Warning About Embedded Code and my Variables" (page 338-339). His conclusion on the matter is that an embedded code construct is in fact a closure.

This means that using a lexical variable inside an embedded code construct in a regular expression binds the instance of the lexical variable in existence at the moment the regex is compiled to the regex. As far as I understand, this means that:

my $regex = qr /( # Start capture \( # Start with '(', (?: # Followed by (?>[^()]+) # Non-parenthesis |(??{ $regex }) # Or a balanced () block )* # zero or more times \) # Close capture )/x; # Ending with ')'

with regard to liz' remark about lexicals at compile time, is "interpreted" (pardon the hand-waving) as:

my $regex = qr /( # Start capture \( # Start with '(', (?: # Followed by (?>[^()]+) # Non-parenthesis |(??{ undef }) # <-- Note! 'or undef' )* # zero or more times \) # Close capture )/x; # Ending with ')'

which will match the innermost parentheses. We don't have any undefs in the target string, so how can this part of the construct match?

Trying to write this down in a sensible manner proved to be quite a challenge, so I apologize if the preceeding section is hard to understand. But to quote Friedl from the aforementioned section: "Warning: this section is not light reading." :o)

Hope this helps.

pernod
--
Mischief. Mayhem. Soap.


In reply to Re: my versus our in nested regex by pernod
in thread my versus our in nested regex by Len

Title:
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, details, 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, summary, 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.