in reply to qr// with /e?

//e is used for replacement, not matching, so it doesn't seem natural to me. This seems pretty simple:
my $patt = join '|', @days; my $days_re = qr/$patt/;
But if you insist on putting it all on one line, try
my $days_re = qr/@{ [join '|', @days] }/;

-Mark

Replies are listed 'Best First'.
Re: Re: qr// with /e?
by tkil (Monk) on Apr 23, 2004 at 22:13 UTC

    My main objection to the two-line method is that it leaves the first value in scope for no good reason. This is exactly why I used the do BLOCK construct in my "workaround", so that the temporary value is never seen outside the assignment.

    And qr// is a bit of an odd duck, anyway; it isn't doing actual matching. In a sense, it is replacing a string with a regex. So I don't find /e all that unnatural.

    But one of the reasons I asked here was to get others' opinion on the matter. Thanks for the feedback, and I'm curious what others will have to say about it. :)

      To avoid the extra variable hanging around, can't you just do:

      my $days_re = join('|', @days); $days_re = qr/$days_re/;

      Of course, I'd like some way to aggregate regular expressions and nicely apply operations like "concatenate" or "alternative" to them without having to drop back into string representations - that is, I'd like to be able to do:

      my @days = qw( Sun Mon Tue Wed Thu Fri Sat ); my $days_re = re_alternative(map {qr/\Q$_\E/} @days);

      Right now you have to do this, which is ok I guess, but I wonder if there are efficiency issues with the regular expression constructed this way:

      my @days = qw( Sun Mon Tue Wed Thu Fri Sat ); my $days_re = join("|", map {qr/\Q$_\E/} @days); $days_re = qr($days_re);
      -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/ map{y/X_/\n /;print}map{pop@$_}@/for@/

        *slaps forehead*

        Yes, re-using the same variable works fine. Grr. Nothing like a bad case of the blindingly obvious, is there?

        In the particular cases you bring up, using quotemeta might be a better option than mapping it to individual protected qr// objects. Not sure it matters all that much.

        As for effeciency, I would hope that perl's regex engine would be smart enough to discard unnecessary non-capturing groups (which is the main side-effect of building up regexes with a bunch of smaller qr//s...)