Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^4: Feature Idea: qr//e

by TheDamian (Vicar)
on Jan 19, 2017 at 22:42 UTC ( [id://1179954]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^5: Feature Idea: qr//e
by choroba (Cardinal) on Jan 19, 2017 at 22:51 UTC
    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!

Re^5: Feature Idea: qr//e
by AnomalousMonk (Archbishop) on Jan 19, 2017 at 22:57 UTC

    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1179954]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-16 18:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found