On May 31, 2004, at 5:33 AM, David Alan Black wrote:

> Hi --
>
> Simon Strandgaard <neoneye / adslhome.dk> writes:
>
>> I think I read somewhere that perl6 uses $ in the regexp replacement 
>> string,
>> in order to avoid our escaping hell.  I think this is a good 
>> initiative.
>> So escape can be used freely:
>>   "hello".gsub!(/h/, %q|\'|)       #->  \'ello
>>   "hello".gsub!(/(.)$1/, '$&$1')   #->  helllo
>>
>> Maybe we should do an RCR..  use dollar in regexp?
>
> Hmmm... I thought Ruby was moving more in the direction of getting rid
> of Perl-like global variables :-) But anyway, I think the thing you're
> describing might lead to some awkward things.  I think it's more 
> logical
> to treat $1 as just a regular variable for purposes of compiling the
> regular expression:
>
>   /(def)/.match("abcdef")
>   puts $1                                 # def
>   /ABC#{$1.upcase}/.match("whatever")
>
> If your change were made, $1 would be nil in the last line because
> there are no captures.
>
> And here, you'd get a kind of postponed compilation of re:
>
>   /(abc)/.match("abc")
>   re = /(blah)#{$1}def/
>
> Sorry for the dumb examples... but I think the problems would be real.

hmm? I think the suggestion has nothing to do with the #{...} construct 
for interpolation. I think Simon is suggesting allowing "$1$2" as an 
alternative to "\\1\\2". These are things that would never be 
interpolated in current scheme. I think this would make things clearer, 
much less confusing. The backslash escaping rules are complicated 
enough that I never use them; I only use the block form of sub and 
gsub.

note that

   /(\d+)$1/.match(str)

would be entirely different from

   /(\d+)#{$1}/.match(str)

as the first would match something like this: "12341234" and the second 
would match "1276#{$1}", with whatever $1 is interpolated as.

cheers,
Mark