sub foo { (my $text = $_[0]) =~ s/$_[1]/$_[2]->($1)/e; print $text; } foo("This is test 1\n", qr/test (.*)/, sub{"fish $_[0]"});