Substitution and interpolation with variables

Answer

my @rules = (
     ['^HELLO (.*)$', 'BONJOUR $1'],
);

foreach my $rule (@rules) {
    if ($string =~ s/$rule->[0]/qq("$rule->[1]")/ee) {
        last;
    }
}

This falls appart if $rule->[1] contains double-quote characters.

There is a simple if rather ugly way around this:

s/$rule->[0]/my $r = eval "<<__EOS__\n$rule->[1]\n__EOS__\n"; chop $r; $r /e

Just about everyone (myself included) who looks at this problem comes up with the double-quote solution first rather than the here-doc solution.

I think this should be in the FAQ. But the maintainers consider it forbidden knowledge that would harm the souls of lesser mortals.

This battle will be the subject of a lightning talk at YAPC::Europe::2004

Next