I've been reading the wisdom of the monks for about 2 weeks now, and decided it was finally time to get an account and post a question that's been bothering me. Here's the problem: I am trying to set up a recursive pattern match for nested parentheses, and since my handy O'Reilly "Programming Perl" had an example of this exact regex, I thought I was going to do OK. Here's my code, slightly modified {in order to match \{ \} instead of \( \) } from its original format on p. 214 in O'Reilly:
local $np; # I added this declaration myself $np = qr{ \{ (?: (?> [^{}]+ ) | (??{ $np }) )* \} }x;
O'Reilly notes that you can't declare the variable in the same statement where it's used, which makes sense to me. So I added the declaration above. Problem is, when I run my code I get this error:
Use of uninitialized value at shorty.pl line 19 (#1) (W) An undefined value was used as if it were already defined. It + was interpreted as a "" or a 0, but maybe it was a mistake. To suppre +ss this warning assign an initial value to your variables. Uncaught exception from user code: / { (?: (?> [^{}]+ ) | (??{ }) )* } /: Sequence (??...) not recognized
The "uninitialized value" warning isn't surprising, since $np is of course uninitialized. It would seem to me that I am screwing up on correctly declaring this variable so that it can be used recursively. While trying to diagnose the problem, I set $np to various values and the error output changed to reflect this. For instance, if I declare $np = "foo"; the error becomes
Uncaught exception from user code: / { (?: (?> [^{}]+ ) | (??{ foo }) )* } /: Sequence (??...) not recognized
So it would seem to me that I am screwing up good code with a bad variable declaration. O'Reilly wouldn't lead me astray, right? :-) So after scouring this and other resources for past info on similar problems, I'm finally stumped enough to announce it publicly on this forum. I am using v. 5.005_02, is it possible that the (??{ }) is not present in this version? I was unable to find much technical documentation for match time pattern interpolation. Thanks to everyone for their time and considerations.

In reply to Problem with recursive pattern matching by tshabet

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.