[cc'ed to -regex b/c this is related to RFC 138]

Proposed replacements for m// and s///:

>    match /pattern/flags, $string
>    subst /pattern/newpattern/flags, $string
> 
> The more I look at that, the more I like it. Very consistent with split
> and join. You can now potentially match on @multiple_strings too.

Just to extend this idea, at least for the exercise of it, consider:

   match;              # all defaults (pattern is /\w+/?)
   match /pat/;        # match $_
   match /pat/, $str;  # match $str
   match /pat/, @strs; # match any of @strs

   subst;              # like s///, pretty useless :-)
   subst /pat/new/;    # sub on $_
   subst /pat/new/, $str;  # sub on $str
   subst /pat/new/, @strs; # return array of modified strings
 
Notice you can drop trailing args and they work just like split. Much
more consistent. This also eliminates "one more oddity", =~ and !~. So
the new syntax would be:

   Perl 5                           Perl 6
   -------------------------------- ----------------------------------
   if ( /\w+/ ) { }                 if ( match ) { }
   if ( $_ !~ /\w+/ ) { }           if ( ! match ) { }            #
better
   ($res) = m#^(.*)$#g;             $res = match #^(.*)$#g;

   next if /\s+/ || /\w+/;          next if match /\s+/ or match /\w+/;
   next if ($str =~ /\s+/) ||       next if match /\s+/, $str or 
           ($str =~ /\w+/)                  match /\w+/, $str;
   next unless $str =~ /^N/;        next unless match /^N/, $str;
   
   $str =~ s/\w+/$bob/gi;           $str = subst /\w+/$bob/gi, $str;
   ($str = $_) =~ s/\d+/&func/ge;   $str = subst /\d+/&func/ge;   #
better
   s/\w+/this/;                     subst /\w+/this/;             

   # These are pretty cool...   
   foreach (@old) {                 @new = subst /hello/X/gi, @old;
      s/hello/X/gi;
      push @new, $_;
   }

   foreach (@str) {                 print "Got it" if match /\w+/, @str;
      print "Got it" if (/\w+/);
   }

Now, this gives us a cleaner syntax, yes. More consistent, more
sensical, and makes some things easier. But more typing overall, and
relearning for lots of people. If it's more powerful and extensible,
then it's worth it, but this should be a conscious decision.

However, it is worth consideration, in light of RFC 138 and many other
issues. If we did eliminate =~, I think something like this would work
pretty well in its place. If anyone thinks this is an idea worthy of an
RFC (the more I look at it the better it looks, but I'm biased :), let
me know. Although we'd probably need something better than "subst".
Maybe just "m" and "s" still.

-Nate

Reply via email to