Perl metaprogramming: when is it unsafe to use quotemeta on the REPLACEMENT value of s///?

后端 未结 3 468
小蘑菇
小蘑菇 2021-01-14 16:25

Perl\'s quotemeta operator typically works on the SEARCH side of s///, but in generating code to be compiled with eval, how should I protect the REPLACEMENT tha

3条回答
  •  忘掉有多难
    2021-01-14 17:01

    The replacement side is a normal interpolating string (unless you start adding /e modifiers, in which case it becomes as many string evals as there are /e modifiers.). Perl 5 does not care what is in the variable you interpolate into the string. It is the same as:

    my $foo = 5;
    my $bar = '$foo';
    my $baz = "$foo $bar"; 
    print "$baz\n"; #this is 5 $foo not 5 5
    

提交回复
热议问题