Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


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; }

-- Hauke D

Update almost 6 months later: I just happened to stumble across this node: qr// with /e? :-)

In reply to Feature Idea: qr//e (updated with solutions) by haukex

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-06-18 18:33 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.