haukex has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
As I was thinking about this node about dynamically building regexes, I had the idle thought that it might be nice if qr// supported qr//e, analogous to s///e it would eval the inside of the construct before parsing it as a regex. Now this is really just a very minor itch, and I don't yet have any idea of how much sense it makes or how difficult it might be to implement, but I still thought I'd bounce it off of you.
Thoughts? Maybe the "normal" and/or "hacked" solutions below are good enough, and the effort required to implement qr//e isn't worth it? Other potential problems I haven't noticed yet?
Update 2017-01-19: TIMTOWTDI has already provided plenty of possible solutions, and I just wasn't feeling creative enough at the moment to see them :-) Thank you very much, LanX, Haarg, and vr! I updated the code with your solutions, and added Test::More and sub testre.
Code:
use warnings; use strict; use Test::More; sub testre; # The "normal" solution my $re = join '|', map {quotemeta} qw/a . | %/; testre qr/$re/i ; # The "hacked" solution testre qr{@{[ join '|', map {quotemeta} qw/a . | %/ ]}}i ; # Wouldn't this be a bit nicer? #testre qr{ join '|', map {quotemeta} qw/a . | %/ }ei ; # ### Update ### # Thanks to LanX sub qre (&;$) { my $re = shift->(); eval 'qr/$re/'.(shift//'') || die $@ } testre qre{ join '|', map {quotemeta} qw/a . | %/ }'i' ; # Thanks to Haarg testre map qr/$_/i, join '|', map {quotemeta} qw/a . | %/ ; # Thanks to vr testre qr{(??{ join '|', map {quotemeta} qw/a . | %/ })}i ; done_testing; sub testre { my $re = shift; diag explain $re; is ref $re, 'Regexp'; like $_, $re for qw/A . | %/; unlike 'bcd', $re; }
Regards,
-- Hauke D
Update almost 6 months later: I just happened to stumble across this node: qr// with /e? :-)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Feature Idea: qr//e
by LanX (Saint) on Jan 18, 2017 at 14:55 UTC | |
by LanX (Saint) on Jan 18, 2017 at 15:19 UTC | |
by haukex (Archbishop) on Jan 18, 2017 at 18:08 UTC | |
by TheDamian (Vicar) on Jan 19, 2017 at 22:42 UTC | |
by choroba (Cardinal) on Jan 19, 2017 at 22:51 UTC | |
| |
by AnomalousMonk (Archbishop) on Jan 19, 2017 at 22:57 UTC | |
Re: Feature Idea: qr//e
by Haarg (Priest) on Jan 19, 2017 at 06:36 UTC | |
by LanX (Saint) on Jan 19, 2017 at 08:02 UTC | |
by haukex (Archbishop) on Jan 19, 2017 at 11:53 UTC | |
by LanX (Saint) on Jan 19, 2017 at 14:03 UTC | |
by haukex (Archbishop) on Jan 19, 2017 at 14:23 UTC | |
| |
by haukex (Archbishop) on Jan 19, 2017 at 11:54 UTC | |
Re: Feature Idea: qr//e
by vr (Curate) on Jan 19, 2017 at 09:50 UTC | |
by haukex (Archbishop) on Jan 19, 2017 at 12:00 UTC | |
Re: Feature Idea: qr//e
by Anonymous Monk on Jan 18, 2017 at 15:17 UTC |