in reply to Re^3: Composing regex's dynamically
in thread Composing regex's dynamically

Why are the dollar signs doubled?

I would not expect the -xism part to cost anything over the (?: ) without that. Again, that makes me wonder if re-compiling the string is worse than incorporating a compiled regex in another qr. Naturally, being able to leave off the parens completely simplifies things.

The regex engine has improved its optimizations in successive versions of Perl too; I think I read something about that in either the 5.10 or 5.12 notes.

Replies are listed 'Best First'.
Re^5: Composing regex's dynamically
by JavaFan (Canon) on Apr 29, 2011 at 09:51 UTC
    Why are the dollar signs doubled?
    Typos. Fixed.
    Again, that makes me wonder if re-compiling the string is worse than incorporating a compiled regex in another qr.
    It's certainly not *worse*. Note that if you do:
    my $re1 = qr/foo/; my $re2 = qr/bar/; my $re3 = qr/$re1|$re2/;
    you compile three patterns, and stringify two. If you write:
    my $re4 = qq/foo/; my $re5 = qq/bar/; my $re6 = qr/$re4|$re5/;
    you compile only one pattern, and stringify none.
      You are asserting that any time you interpolate a compiled re into another, it just stringifies the first anyway, and re-compiles the whole thing?

      I had always assumed that it copies the compiled bits and does not need to re-process it. I formed that opinion in the early days of the existence of qr, so that was a long time ago now.

        You are asserting that any time you interpolate a compiled re into another, it just stringifies the first anyway, and re-compiles the whole thing?
        That's no assertion. That's how it works.
        I had always assumed that it copies the compiled bits and does not need to re-process it.
        No, that's not really possible to do (think for instance paren counts - or consider this: $q = qr/x/; $r = qr/[$q]/). It only works in this case:
        my $qr1 = /pattern/; my $qr2 = /$qr1/;
        Use use re 'debug' and you will see what strings are being compiled.
        What JavaFan said
        $ perl -Mre=debug -e " $f = qr/./; warn if m/$f/" Compiling REx "." Final program: 1: REG_ANY (2) 2: END (0) minlen 1 Freeing REx: "." $ perl -Mre=debug -e " $f = qr/./; $g = qr/$f/; warn if m/$g/" Compiling REx "." Final program: 1: REG_ANY (2) 2: END (0) minlen 1 Freeing REx: "."
        $ perl -Mre=debug -e " $f = qr/./; $g = qr/./; $c=qr/$f$g/; " Compiling REx "." Final program: 1: REG_ANY (2) 2: END (0) minlen 1 Compiling REx "." Final program: 1: REG_ANY (2) 2: END (0) minlen 1 Compiling REx "(?-xism:.)(?-xism:.)" Final program: 1: REG_ANY (2) 2: REG_ANY (3) 3: END (0) minlen 2 Freeing REx: "." Freeing REx: "." Freeing REx: "(?-xism:.)(?-xism:.)"