tshabet has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Problem with recursive pattern matching
by Hofmator (Curate) on Jul 26, 2001 at 18:39 UTC

    I am using v. 5.005_02, is it possible that the (??{ }) is not present in this version?
    Yup, this is the case. At least I would guess that from your error message - I'm not sure when this feature was introduced but it is quite recent.

    -- Hofmator

Re: Problem with recursive pattern matching
by Tyke (Pilgrim) on Jul 26, 2001 at 18:42 UTC
    from perlre
    (??{ code })
    WARNING: This extended regular expression feature is considered highly experimental, and may be changed or deleted without notice.

    But there again I have an old version of perlre ;)

      The (??{code}) construct was introduced in perl 5.6.0. As far as I know, it's still considered experimental.

Problem with recursive pattern matching
by tshabet (Beadle) on Jul 26, 2001 at 19:09 UTC
    Aaaah, there we go. Thanks guys, I'm updating my version right now. Here's hoping that "exprerimental" is secret monkese for "extremely reliable." :-) Thanks so much for your help.