1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#!/usr/bin/perl use strict; use warnings; my $text; while(<DATA>) { $text=$text.$_; } my $repl={ foo_bar => { is => "foo", by => "bar" }, MS => { is => "rock", by => "suck" }, id => { is => "\{id\}(?<id>[^\{]+)\{\/id\}", by => "ID: $+{id}" }, }; foreach my $p (keys $repl) { $text =~ s/$repl->{$p}->{'is'}/$repl->{$p}->{'by'}/g; } print $text, "\n"; __DATA__ My name is foo, yes foo. {id}1234{/id} Microsoft rocks!
1 2 3 4
my $repl = { id => { is => ..., by => sub { "ID: $+{id}" } }, }; $text =~ s/$repl->{$p}->{is}/$repl->{$p}->{by}->()/eg;