Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Feature Idea: qr//e

by LanX (Cardinal)
on Jan 18, 2017 at 15:19 UTC ( #1179861=note: print w/replies, xml ) Need Help??


in reply to Re: Feature Idea: qr//e
in thread Feature Idea: qr//e (updated with solutions)

Having your other post in mind I misread your question, sorry.

you don't want to operate on an array but a code-block

Would you accept something like the following as sufficiently equivalent?

sub qre(&;@) { my $block = shift; my $str = $block->(@_); return qr/$str/; }

(not tested)

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

udpate

some functional code:

use Data::Dump; sub qre(&;@) { my $block = shift; my $str = $block->(@_); return qr/$str/; } sub ored { return join '|', map {quotemeta} @_ } sub oredraw { return join '|', @_ } my @strings = qw/. | %/ ; dd qre {join '|', map {quotemeta} @strings }; dd qre \&ored, @strings; dd qre \&oredraw, @strings;

Replies are listed 'Best First'.
Re^3: Feature Idea: qr//e
by haukex (Bishop) on Jan 18, 2017 at 18:08 UTC

    Hi LanX,

    sub qre(&;@) { my $block = shift; my $str = $block->(@_); return qr/$str/; }

    Thanks, that's an excellent piece of inspiration! Just to take that idea a little further and add support for /i and the like:

    sub qre (&;$) { my $re = shift->(); eval 'qr/$re/'.(shift//'') || die $@ } my $regex = qre{ join '|', qw/foo bar/ }'i'; print "$regex\n"; __END__ (?^i:foo|bar)

    Not that I'm going to start using this right away, for now this is just to satisfy my curiosity ;-)

    Thanks,
    -- Hauke D

      One extra tweak to the prototype would allow you to add trailing modifiers without requiring the pesky quotes around them:
      sub qre (&;*) { my $re = shift->(); eval 'qr/$re/'.(shift//'') || die $@ } my $regex = qre{ join '|', qw/foo bar/ }i; print "$regex\n"; __END__
      or, from Perl 5.20 onwards:
      sub qre :prototype(&;*) { my $re = shift->(); eval 'qr/$re/'.(shift//'') || die $@ } my $regex = qre{ join '|', qw/foo bar/ }i; print "$regex\n";
      Damian
        It works only for some of the modifiers:
        my $regex = qre{ join '|', qw/foo bar/ }m;

        Returns:

        Global symbol "$regex" requires explicit package name at ./1.pl line 1 +1. Execution of ./1.pl aborted due to compilation errors.

        Similarly, s yields

        Global symbol "$regex" requires explicit package name at ./1.pl line 1 +1. syntax error at ./1.pl line 14, at EOF (Might be a runaway multi-line ;; string starting on line 11) Execution of ./1.pl aborted due to compilation errors.
        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        Replacing the expression  (shift//'') with  (@_ ? shift : '') gives you 5.10 agnosticism.


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1179861]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2021-01-19 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?