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

Re^3: Feature Idea: qr//e

by haukex (Bishop)
on Jan 18, 2017 at 18:08 UTC ( #1179869=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^4: Feature Idea: qr//e
by TheDamian (Priest) on Jan 19, 2017 at 22:42 UTC
    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,

        ++choroba

        You're quite right: not a good tweak when it's not 100% applicable.

        Of course, you could still write:

        my $regex = qre{ join '|', qw/foo bar/ }m =>;
        but that's probably just adding insult to injury.

        And, yes, I was indeed caught out by this because I no longer even think about the m or s modifiers.
        Especially now that I can add a standard:

        use re '/xms';
        at the top of every Perl file. ;-)

        Damian

        Hi choroba,

        Hm, the * prototype is a really cool idea, too bad about that... here's a less elegant workaround (also incorporates AnomalousMonk's suggestion). As far as I can tell, for single letter modifiers it's only m and s that cause a problem, the others (/ixpodualn) are fine, and multi-letter modifiers are fine as far as they don't clash with any other subs (like main, min, or sum) or operators (like and, although that's not a valid combination of modifiers). With the letters "msixpodualn" you can actually spell a lot of words :-)

        use List::Util qw/min sum/; sub qre (&;*) { my $re = shift->(); eval 'qr/$re/'.(@_ ? lc shift : '') || die $@ } say qre{ join '|', qw/foo bar/ }M; say qre{ join '|', qw/foo bar/ }msx; say qre{ join '|', qw/foo bar/ }Min; say qre{ join '|', qw/foo bar/ }Sum; __END__ (?^m:foo|bar) (?^msx:foo|bar) (?^min:foo|bar) (?^ums:foo|bar)

        Regards,
        -- Hauke D

        Oops...

        Well, qr//s should always be written with a standard /xms tail anyway, so

        c:\@Work\Perl>perl -wMstrict -le "sub qre (&;*) { my $re = shift->(); return eval 'qr/$re/xms' . (@_ ? shift : '') or die $@; } ;; my $regex = qre{ join '|', qw/foo bar/ }i; print $regex; " (?msix:foo|bar)
        :-))


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

        Most probably the parser gets confused because m and s are builtin commands on their own and can't be bare words.

        In other words

        sub m {} should be illegal.

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

      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://1179869]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2021-01-24 00:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?