my %subs = ( word1 => 'replace word1 with this text', word2 => 'replace word2 with this test', ); while($document =~ s/\[(\w+)\]) { my $word = $1; my $rep = $subs{$word} || "!Unknown token: $word!"; $document =~ s/\[$word\]/$subs{$word}/smg; }