develooper Front page | perl.perl5.porters | Postings from February 2015

RFC: extended regex modifiers similar to Perl 6 modifiers

Thread Next
From:
Brad Gilbert
Date:
February 15, 2015 18:05
Subject:
RFC: extended regex modifiers similar to Perl 6 modifiers
Message ID:
CAD2L-T0Dy-6ExZDZpjtFOqKrgat2kzyrXfhRW5VwSEzOBCoL6A@mail.gmail.com
On Fri, Feb 13, 2015 at 11:48 PM, demerphq <demerphq@gmail.com> wrote:
> On 13 February 2015 at 21:58, Ricardo Signes <perl.p5p@rjbs.manxome.org> wrote:
...
>> That said, I think we're at the point where we should be looking for non-flag
>> modifiers for our regex, or long-form flags.  /mxyzptlk is all well and good
>> for joking around, but sometimes it's nice to be able to read one's code, too!
>
> I think if we are going to go there then we should add new keywords
> and do this via a functional(ish) interface.
>
> So something like this:
>
> match( /PATTERN/, case_insensitive  => 1, extended_mode => 1,
> dot_matches_newline => 1, multiline => 1);
>
> (Those options correspond to /i /x /s and /m respectively)
>
> and something like this:
>
> search_and_replace(/PATTERN/, "REPLACEMENTS", global => 1, eval => 1,
> extended_eval =>1);
>
> (those options correspond to /g /e and /ee - extended_eval would imply
> eval, so the eval=>1 there is redundant)
>

Perhaps there is a page to be taken from Perl 6

In Perl 6 it would be written something like:

    s:global:ignorecase/PATTERN/{ EVAL "REPLACEMENTS" }/;
    s:g(True):ignorecase(True)/PATTERN/{ EVAL "REPLACEMENTS" }/;

So I think a way of writing it in Perl 5 could be something like:

    s/PATTERN/"REPLACEMENTS"/ :eval(2) :extended :ignorecase ;
    s/PATTERN/"REPLACEMENTS"/ :e(2) :x :i ;
    s/PATTERN/"REPLACEMENTS"/ :eval :eval :x :i(1) ;

A benefit of this addition is that it is currently a syntax error.

If you want to disable one of the options:

    use re '/:ignorecase';
    s/PATTERN/"REPLACEMENTS"/ :!i ;
    s/PATTERN/"REPLACEMENTS"/ :i(0) ;
    s/PATTERN/"REPLACEMENTS"/ :!ignorecase ;

All of the `:i(1)` type modifiers would have to be determinable at compile time,
but an arbitrary expression could possibly be allowed.

    use constant IC => 1;
    s/[a-z]/_/ :ignorecase(IC);
    s/[a-z]/_/ :ignorecase(IC*2);

I have absolutely no idea how much trouble any of this would
be to implement, it is just an idea I had.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About