Re: $foo.s/foo/bar/
On Thu, 2004-05-13 at 04:30, Rafael Garcia-Suarez wrote: > Aaron Sherman wrote: > > Is it a special type of calling convention, e.g.: > > > > sub s (Regex $pat, Str $replace, bool ?$i) is doublequotelike returns(Str) { > > Ooh, "doublequotelike" sounds so much 1984. > (Moreover it doesn't describe accurately the reality, which allows to > use different delimiters for the pattern and the replacement.) Hmm... I think doublequotelike would work, as long as it were a superset of both sets of behaviors and the object that you get is neither a Regex nor a Str, but mutable into both (a sort of pre-parsed doublequotelike thing) and then in this case, you could optimize the conversion of both parameters, because you know what they'll be converted into. For example: role doublequotelike { has Parser::Perl6::ProtoString $.string; # Not sure how to say "conversion to Str calls this method" method as_string() returns(Str) {...} method as_regex() returns(Regex) {...} } doublequotelike.string would just contain a series of tokens (e.g. m/\s+$pattern/ yields (literal => '\s+', interpolate => '$pattern')). In this way, you can apply your Perl 5 or Perl 6 mode by simply deriving a new doublequotelike class. Is there some reason not to do that that I'm missing? Why use a macro here when that prevents you from ever using it as a method? More to the point, even if it is a macro, would this kind of facility be allowed for creating a method with the same syntax exposed to the user. Performing a regex on an object seems so common an idiom that I don't think it's healthy to make it such a special case. -- Aaron Sherman <[EMAIL PROTECTED]> Senior Systems Engineer and Toolsmith "It's the sound of a satellite saying, 'get me down!'" -Shriekback
Re: $foo.s/foo/bar/
Aaron Sherman wrote: > Is it a special type of calling convention, e.g.: > > sub s (Regex $pat, Str $replace, bool ?$i) is doublequotelike returns(Str) { Ooh, "doublequotelike" sounds so much 1984. (Moreover it doesn't describe accurately the reality, which allows to use different delimiters for the pattern and the replacement.)
Re: $foo.s/foo/bar/
Juerd wrote: Juerd skribis 2004-05-12 20:15 (+0200): But I think I still want to have some non-mutating version of s/// that returns the modified string, so that you can just write something like print s:gx/\w+/WORD/ for <>; Actually, can't we just use the . for s///? You'd then use $foo.s/// to get the new string ang $foo.=s/// to mutate $foo. Working from the other direction, parens are not valid pattern delimiters, leaving s() open for use: print s(/:g \w+/, 'WORD'); (Or somesuch...dunno about the positioning of :g.) -- Brent "Dax" Royal-Gordon <[EMAIL PROTECTED]> Perl and Parrot hacker Oceania has always been at war with Eastasia.
Re: $foo.s/foo/bar/
Aaron Sherman writes: > On Wed, 2004-05-12 at 14:22, Juerd wrote: > > > Actually, can't we just use the . for s///? > > Well, that brings up something that I don't think Larry has covered yet. > That is, it brings into question what s/// *is* in the grammar. Well, I imagine it's just a macro called C that turns itself into an appropritate sub call. As a method, I don't think it exists. It's more of a $str.subst(). You can't add special parse rules to methods, since you can't do polymorphic parsing[1]. So you'd more likely have: my $subd = $str.subst( rx/ \w+ / => "WORD", ); Which would open up: my $subd = $str.subst( rx/ I\< (.*?) \> / => { "$1" }, rx/ \< / => '<', rx/ \> / => '>', ); Ã la Regexp::Subst::Parallel. Provided the hypothetical scoping works out there. Hmm, would the scope of $1 propagate? Luke [1] I feel a new experimental language coming on... >:-}
Re: $foo.s/foo/bar/
On Wed, 2004-05-12 at 14:22, Juerd wrote: > Actually, can't we just use the . for s///? Well, that brings up something that I don't think Larry has covered yet. That is, it brings into question what s/// *is* in the grammar. Is it a special type of calling convention, e.g.: sub s (Regex $pat, Str $replace, bool ?$i) is doublequotelike returns(Str) { bool $did = false; if my $match = ($CALLER::_ =~ m:i($i)/$pat/) { $match = $replace; $did = true; } return $did; } or is it something more deeply buried in the parser? If it's just quoting, then that's (relatively) easy: class String { ... method s (...) is doublequotelike ... {...} } Otherwise, you would have to bury this deep in the parser as a special case with the definition of s/// and that seems of questionable value for the complexity you add. -- Aaron Sherman <[EMAIL PROTECTED]> Senior Systems Engineer and Toolsmith "It's the sound of a satellite saying, 'get me down!'" -Shriekback
$foo.s/foo/bar/
Juerd skribis 2004-05-12 20:15 (+0200): > But I think I still want to have some non-mutating version of s/// that > returns the modified string, so that you can just write something like > print s:gx/\w+/WORD/ for <>; Actually, can't we just use the . for s///? You'd then use $foo.s/// to get the new string ang $foo.=s/// to mutate $foo. For bare s///, I think copying is more useful than mutating. I'm not sure, and am posting this before actually thinking about it :) Juerd