in reply to Re: variable interpolation in regexps
in thread variable interpolation in regexps

With respect I don't think it is seeing $ as an RE meta-character, but interpolation is looking for a variable called $thing. My understanding is that $ only means 'end-of-text' at the end of an RE expression, unless /m is specified.
Using \Q will still fix it though.
  • Comment on Re^2: variable interpolation in regexps

Replies are listed 'Best First'.
Re^3: variable interpolation in regexps
by Corion (Patriarch) on Nov 24, 2006 at 15:38 UTC

    I now realize that I only saw half of the problem. The wanted idea seems to be double-interpolation of 'some$thing' into "someTHING" by replacing the string '$thing' within $regexp with the value of the variable $thing.

    Your points are close but wrong - Perl allows stuff to appear in regular expressions after the $ metacharacter. Otherwise, Perl regular expressions act like double-quoted strings and hence interpolate only once:

    #!perl -wl use strict; my $regexp = 'some$thing'; my $thing = 'THING'; my $target = 'this is some$thing strange.'; print $regexp; print $target; print '$target =~ /$regexp/ ', $target =~ /$regexp/; print '$target =~ /\Q$regexp\E/ ', $target =~ /\Q$regexp\E/; print '$target =~ /some\$thing/ ', $target =~ /some\$thing/; print '$target =~ /some$/ ', $target =~ /some$/; my $eol_in_the_middle = qr/some$(?:thing)/; print 'eol_in_the_middle ',$eol_in_the_middle; print '$target =~ /$eol_in_the_middle/ ',$target =~ /$eol_in_the_middl +e/;

    Of course, it's kinda hard to make $eol_in_the_middle match, but you can do it by using the /m switch and changing the RE and target string a bit:

    my $eol_in_the_middle2 = qr/some$(?:\s*thing)/; my $target2 = "this is some\nthing strange."; print '$target2 =~ /$eol_in_the_middle2/ ',$target2 =~ /$eol_in_the_mi +ddle2/sm;