my $repl = { id => { is => ..., by => sub { "ID: $+{id}" } }, }; $text =~ s/$repl->{$p}->{is}/$repl->{$p}->{by}->()/eg;