nikolay has asked for the wisdom of the Perl Monks concerning the following question:

Have a good time of the day.

How do i accomplish an exchange of «йц,» to «фы,» as follows:

use utf8::all;
$sod='йц,';
$a=q~(?^u:йц(\W))~;
#$b=qq~фы$1~;
$b=q~фы$1~;
print "$sod\n";
$sod=~s#$a#$b#g;
print "$sod\n";

? I expect that regexp turns «йц,» to «фы,», but actually i get «йц,» turns to «фы» or to «фы$1» -- depending on whither i use $b w/ double or single quatation.

Replies are listed 'Best First'.
Re: Regexp interpolation.
by choroba (Cardinal) on Sep 16, 2015 at 13:44 UTC
    In order to evaluate the replacement as code, you have to use the /e modifier:
    #!/usr/bin/perl
    use warnings;
    use strict;
    use utf8;
    
    my $from = qr/йц(\W)/;
    my $to   = '"фы$1"';
    
    my $string = 'йц,';
    $string =~ s/$from/$to/ee;
    
    binmode STDOUT, 'utf8';
    print $string, "\n";
    
    Output:
    фы,
    
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ