在Perl中,操作符s /用于替换字符串的部分。现在s /将改变它的参数(字符串)到位。然而,我想替换打印它的字符串的部分,如同
print "bla: ",replace("a","b",$myvar),"\n";
有没有这样的替换功能在Perl,还是其他一些方法呢?在这种情况下,s /将不会直接工作,我想避免使用帮助变量。有没有办法这样做呢?@H_502_4@
未经测试:
require 5.013002; print "bla: ",$myvar =~ s/a/b/r,"\n";
见perl5132delta:@H_502_4@
The substitution operator now supports a /r option that copies the input variable,carries out the substitution on the copy and returns the result. The original remains unmodified.@H_502_4@
my $old = 'cat'; my $new = $old =~ s/cat/dog/r; # $old is 'cat' and $new is 'dog'