New match and subst replacements for =~ and !~ (was Re: RFC 135 (v2) Require explicit m on matches, even with ?? and // as delimiters.)

2000-08-25 Thread Nathan Wiger

[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



Re: New match and subst replacements for =~ and !~ (was Re: RFC 135 (v2) Require explicit m on matches, even with ?? and // as delimiters.)

2000-08-28 Thread Randy J. Ray

> 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
 
For the takes-an-array variants of match and subst, what becomes of
back-references? Does

subst PAT, LIST

modify LIST in-place, or return the result of modification? What does

match PAT, LIST

return at all, and what of multiple back-references in PAT?

># 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+/);
>}

This implies that the subst keyword would *both* modify LIST in-place and
return a complete copy of the list as a return-value. Is this correct?
Additionally, the match example above is not identical on each side-- the
Perl 5 version would print "Got it" for *every* matching element of @str,
whereas the Perl 6 version would print it just once. While you could change
the Perl 5 example to fit (by adding "last"), I just don't know that the
blind application of a regex to a list has a place here.

Randy
--
---
Randy J. Ray  | Programming is a Dark Art [...] The programmer is fighting
[EMAIL PROTECTED]  | against the two most destructive forces in the universe:
415-777-9810 x246 | entropy and human stupidity. --Dr. Damian Conway



Re: New match and subst replacements for =~ and !~ (was Re: RFC 135 (v2) Require explicit m on matches, even with ?? and // as delimiters.)

2000-08-28 Thread Nathan Wiger

"Randy J. Ray" wrote:
> 
> ># These are pretty cool...
> >foreach (@old) { @new = subst /hello/X/gi, @old;
> >   s/hello/X/gi;
> >   push @new, $_;
> >}
> >
> This implies that the subst keyword would *both* modify LIST in-place and
> return a complete copy of the list as a return-value. Is this correct?

No, @old wouldn't be modified because $_ is a temporary value that has
no inherent connection to @old. So modifying $_ won't change @old. THAT
code is correct...

> >foreach (@str) { print "Got it" if match /\w+/, @str;
> >   print "Got it" if (/\w+/);
> >}
> 
> Additionally, the match example above is not identical on each side-- the
> Perl 5 version would print "Got it" for *every* matching element of @str,
> whereas the Perl 6 version would print it just once.

But this example's broken. Sorry for the confusion. What I *meant* was:

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

Now if DWIM just worked for email as well... ;-)

v2 of the RFC should be out tomorrow and should make a lot more sense.
It will be posted to perl6-language-regex.

-Nate



Re: New match and subst replacements for =~ and !~ (was Re: RFC 135 (v2) Require explicit m on matches, even with ?? and // as delimiters.)

2000-08-29 Thread Bart Lateur

On Mon, 28 Aug 2000 20:26:41 -0700, Nathan Wiger wrote:

>   foreach (@str) { print "Got it" if match /\w+/, @str;
>  if (/\w+/) { $gotit = 1 };
>   }
>   print "Got it" if $gotit;
>
>Now if DWIM just worked for email as well... ;-)

You mean, like grep?

print "Got it" if grep /\w+/, @str;

This one *currently works*. No need for a new feature.

-- 
Bart.