Here is a variation on how to do it, which is almost identical to your code with a slight difference.
use strict;
use warnings;
sub TemplateReplace {
my($regex, $replacement, $text) = @_;
$$text =~ s/($regex)/$replacement/gs;
}
my $text = "This is a test.";
TemplateReplace("test", "banana", \$text);
print $text;
This behavior is explicit instead of implicit. In practice, it works identically to Chas. Owens result, but uses scalar-refs instead of relying on understanding the behaviour of arrays.
This will make it more obvious to anybody reading your code that the function "TemplateReplace" is intentionally modifying $text.
Additionally, it will tell you you're using it wrong by squawking with :
Can't use string ("This is a test.") as a SCALAR ref while "strict refs" in use at replace.pl line 9.
If you happen to forget the \ somewhere.