in reply to Re^4: Interpolate into replacement with s//?
in thread Interpolate into replacement with s//?

It doesn't matter where the string came from. Notice he changed Z$1Z (not Perl code) to "Z$1Z" (Perl code). So you'd need to either change the file or add quotes to what you read from the file.

Replies are listed 'Best First'.
Re^6: Interpolate into replacement with s//?
by philkime (Beadle) on Nov 10, 2011 at 09:14 UTC
    I can't seem to work out the quoting when the string is from elsewhere:
    use 5.014000; use Config::General; my $conf = new Config::General({replace => 'Z\l$1Z'}); my $string = q/ABC/; my $search = qr/^(A)/; say $conf->{config}{replace}; my $replace = q/$conf->{config}{replace}/; $string =~ s/$search/$replace/gee; say $string;
    gives:
    Z\l$1Z Z\l$1ZBC
    I've tried all sorts of combinations of quoting and regexp flags but I can't seem to get it to work when $replace isn't explicitly assigned as in your example.

      Z\l$1Z is not "Z\l$1Z". Please reread what I wrote.

        I see that but how would you quote in my last example where the replacement comes from an external source? It's not so obvious in this case ...