in reply to Programmatic regex disjunction

Also, in my code the separate patterns are compiled much before the disjunction, so this is not what I’m looking for:

But that is what you get. You cannot combine the compiled forms of precompiled regexen, they must be stringified for concatenation and the result has to be recompiled. There’s no way around that.

So the last snippet is what you want. Sorry.

Btw, the stringified regexen supply their own (?:) delimitation, so all you need to do is

my $joined = do { local $_ = join '|', @any; qr/$_/; };

As an aside, you may be interested in Regexp::Assemble.

Update: added do as per gaal’s reply.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^2: Programmatic regex disjunction
by Roy Johnson (Monsignor) on Nov 21, 2005 at 17:31 UTC
    I didn't like the use of $_ to build a pattern, just as a matter of taste. I figure if you're going to localize a variable for this, make it the auto-joiner, taking advantage of the fact that regex context is double-quotish for variable interpolation (it would make sense to me if Perl magically set $" for array interpolation):
    my $joined = do { local $" = '|'; qr/@pats/; };
    though I'd likely do it like this:
    my $joined = qr/${\join '|', @pats}/;

    Caution: Contents may have been coded under pressure.
Re^2: Programmatic regex disjunction
by ysth (Canon) on Nov 22, 2005 at 04:14 UTC
    You cannot combine the compiled forms of precompiled regexen, they must be stringified for concatenation and the result has to be recompiled. There’s no way around that.
    There is; that is exactly what (??{$someregex}) does.

      Ah! Of course.

      But a quick test shows me that that too does not propagate captures from the delayed patterns to the including pattern, which is why I memorised the rule as “combining precompiled patterns requires recompilation.”

      (Most of the time I build a pattern programmatically from a list involves matching one of a list of hash keys, some way or other, so capturing is always involved in some fashion.)

      Thanks for the correction.

      Makeshifts last the longest.

Re^2: Programmatic regex disjunction
by gaal (Parson) on Nov 21, 2005 at 17:20 UTC
    Don't you need a do there?

    Nowadays = { ... } looks like a closure assignment to me because of Perl 6.