Jonathan Scott Duff <[EMAIL PROTECTED]> writes: > On Wed, Nov 19, 2003 at 08:23:30PM +0000, Smylers wrote: >> This, however, is irritating: >> >> my @new = map { s:e/$pattern/$replacement/; $_ } @old; >> >> I forget the C<; $_> far more often than I like to admit and end up with >> an array of integers instead of modified strings. > > That one gets me every now and then too. > >> So I'd like a more elegant way of writing that -- but I don't think >> making the return value of C<s///> more complicated (and duplicating >> data in the process) would be a nett gain. > > What if the method form of s/// didn't mutate the string, but returned > the mutated result? Then, you'd just need to do something like: > > my @new = map { $_.s:e/$pat/$rep/ } @old > > Except I don't know how the method form of s/// would be spelt. > > $string.s:e/$pat/$rep/; # . and ~~ are almost the same? > $string.sub($pat,$rep,"each"); # ick. > $string.sub:e($pat,$rep); # hmm.
aString replace: aPattern with: aString. aString replaceAll: aPattern with: aString. Except... the second argument isn't strictly a string because it's evaluated in the match context. Assuming we get a pure functional form of the substitution operator, then scoping's going to be fun. If you want a simple form with the signature: method String::sub( Rule $pattern, String $replacement ) you're almost certainly going to have to implement that as a macro so you can get at the string before it's interpolated.