in reply to Re^5: qr// with /e?
in thread qr// with /e?

However the compiler cannot stitch together subpatterns in compiled form, so whenever you build up a pattern from two previously qr'd patterns, or a qr'd pattern and some new text, perl constructs the stringified form of the new pattern and then compiles this new string from scratch.

Interesting! I wonder if there is a need for a low-level regex-manipulating package that would allow us to do manipulations like this on the actual regex objects. (Or if such an interface already exists!)

One comment / question on your preferred solution:

my $days_re = join '|', map "\Q$_\E", @days;

I've seen this used a few times now, and I wonder if there is a meme out there people are copying from. Is there any reason to prefer this over:

my $days_re = join '|', map quotemeta, @days;

Replies are listed 'Best First'.
Re: Re^6: qr// with /e?
by hv (Prior) on Apr 26, 2004 at 09:14 UTC

    I guess I think first of the \Q...\E form because I rarely quote things quite like this - more often I'm quoting a fragment inside a larger string.

    Precisely because of that though, when reading the code you need to look more carefully at a \Q...\E construct, so quotemeta would have been the better thing to use in this case.

    Note that this is a "belt and braces" approach: the quotemeta() isn't actually required for the strings specified, so this is a protection against future changes to the apparently constant data (and relatively unlikely changes as well, since I can't think of any languages that use regexp metacharacters in their names for the days of the week).

    Hugo