http://qs321.pair.com?node_id=788422


in reply to un-Nifty qr OR ASCII bullet parser

Safer (allows "-", for example):
my $bullet = sprintf qr/[%s]/, quotemeta join '', keys %bullets;
Alternative (shorter and returns a compiled regexp unlike the OP's and the above solutions):
my ($bullet) = map qr/[\Q$_\E]/, join '', keys %bullets;

Replies are listed 'Best First'.
Re^2: Nifty qr OR bullets with butterfly wings
by belg4mit (Prior) on Aug 13, 2009 at 20:00 UTC
    Alternative (shorter and returns a compiled regexp unlike the OP's and the above solutions):
    Huh? My code returns (?-xism:[@*+]), but the inclusion of \Q is a good idea.

    --
    In Bob We Trust, All Others Bring Data.

      It returns the string (?-xism:[@*+])

      1. qr/[%s]/ builds a regex that matches strings containing "%" or "s".
      2. sprintf stringifies the compiled regex to (?-xism:[%s]) to use it as the format pattern.
      3. The characters are included into the format pattern and the result is returned by sprintf.

      So not only do you not end up with a compiled pattern, you waste time compiling a pattern you never use!

        Doh! damn, that sucks.... back to $bullet = qr/[\Q@{[join '', keys %bullets]}\E]/; I guess.

        --
        In Bob We Trust, All Others Bring Data.