[perl] perl string substitution

 

require 5.013002; # or better:    use Syntax::Construct qw(/r);
print "bla: ", $myvar =~ s/a/b/r, "\n";

See perl5132delta:

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.

my $old = 'cat';
my $new = $old =~ s/cat/dog/r;
# $old is 'cat' and $new is 'dog'


来源:https://stackoverflow.com/questions/3440363/perl-use-s-replace-and-return-new-string#3440474
posted @ 2024-11-15 17:58  profesor  阅读(2)  评论(0编辑  收藏  举报