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