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

I've never researched that, but if I make building blocks to assemble regexes, I typically surround that with (?:), unless it's not going to matter. So, I'd write:
my $bblock1 = "[a-f]"; my $bblock2 = "foo"; my $bblock3 = "(?:$bar|$baz)";
And then there's:
my $bblock4 = "[a-i]"; my $bblock5 = "[A-I]"; my $bblock6 = "[0-9]"; my $bblock7 = "$bblock4|$bblock5|$bblock6"; $bblock7 =~ s/\Q]|[\E//g; # Make one range

Replies are listed 'Best First'.
Re^4: Composing regex's dynamically
by John M. Dlugosz (Monsignor) on Apr 28, 2011 at 23:37 UTC
    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.

      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.