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

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.

Replies are listed 'Best First'.
Re^6: Composing regex's dynamically
by John M. Dlugosz (Monsignor) on Apr 30, 2011 at 08:40 UTC
    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:.)"