From: Mike Stok Date: 2010-09-27T11:03:25+09:00 Subject: Re: bug in gsub(?) On Sep 26, 2010, at 9:40 PM, Chad Perrin wrote: > On Sun, Sep 26, 2010 at 03:54:35PM +0900, Brian Candler wrote: >> Chad Perrin wrote: >>> I've wondered for quite a while what was the rationale for having \1 in >>> the first place. >> >> Ruby inherits a lot from Perl, and Perl from sed. > > Okay . . . I guess that sorta makes sense. Of course, I've never used \1 > in Perl, nor seen anyone else do so either, so until you mentioned it I > had entirely forgotten that was an option there either. > > Both languages would be better off without that syntax, and just stick > with $1 instead, I think. > > >> >> Some of the Perlisms are IMO superfluous - in particular the Kernel >> methods which operate on $_, and the flip-flop conditional operators. > > I wouldn't really call \1 a "Perlism", given that the way I've always > seen it done is with $1 instead. If it's a Perlism despite its lack of > general usage, I'd say it's every bit as much a Rubyism. There are times in Perl when you need to use \1 in the matching part of a regular expression because you don't want $1 to interpolate into the match. Consider trying to match a simple quoted string (i.e. no \ escaping): my $s1 = "Hello there"; my $s2 = q{The cat said "Hello there, how's it going?"}; if ($s1 =~ m/(ell)/) { print "print s1 matched - \$1 is '$1'\n"; } if ($s2 =~ m/(["'])(.*?)\1/) { print "print s2 matched - \$2 is '$2'\n"; } This outputs: print s1 matched - $1 is 'ell' print s2 matched - $2 is 'Hello there, how's it going?' If you try using $1 in place of \1 in the second regex then it will output print s1 matched - $1 is 'ell' print s2 matched - $2 is 'H' Mike -- Mike Stok http://www.stok.ca/~mike/ The "`Stok' disclaimers" apply.