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

The following sentance appears in perlre in reference to the (?{...}) and (??{...})

...the rules to determine where the code ends are somewhat convoluted.

anyone care to try for an explanation of what this means exactly (or even roughly)?


Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.

Replies are listed 'Best First'.
Re: Perlre interpretation required
by jryan (Vicar) on May 02, 2003 at 21:24 UTC

    It means that the rules for parsing (?{}) and (??{}) are different than parsing normal perl code. The main difference is that all brackets must be either backslashed or balanced. For instance, this re is invalid:

    /(??{ "\d+" if $1 eq "{" })/

    It would need to be changed to:

    /(??{ "\d+" if $1 eq "\{" })/

      Thankyou. That explains not only the text, but some peculiarities I have been encountering and couldn't get to the bottom of.

      I sincerly think your explaination deserves adding to perlre.


      Examine what is said, not who speaks.
      1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
      2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
      3) Any sufficiently advanced technology is indistinguishable from magic.
      Arthur C. Clarke.
        I might have encountered Peculiarities in using (??{...}) too, and I intended to inquire about them, but didn't want to deal with it yet (guess I'm about to though!). I wanted to match either non-single or non-double quote characters in a regex, depending on which type of quotes surrounded it. For example, since
        $var =~ /(["'])[^\1]*?\1/;

        doesn't work due to [^\1] not working, I tried something like
        $var =~ /(["'])[^(??{"$1"})]*?\1/;
        which worked unless $1 contained a special character (forgot the character, but it wasn't a quote), when it gave me an error. I tried various fixes, and finally got
        $var =~ /(["'])([^"']|[^(??{"$1"})])*?\1/;
        to work. I figured the [^"'] alternative that I added would match the problem character, eliminating the need for parsing the (??{"$1"}) construct, which had given me the error. Well, it worked until it didn't, for whatever reason. I think I created another patch (placed \Q and \E somewhere), which didn't work consistantly either. Then I spent several hours deleting all of the (??{...}) constructs and stuck with a simple .*? between the quotes. I'm just a beginner, so it's possible I did something wrong, but I don't know what.

      No - you only have to do that if the curly brackets are unbalanced. If you use only balanced brackets then you don't have to escape them.

        Isn't that what I just said? (-:
        The main difference is that all brackets must be either backslashed or balanced.