my $text = 'this is my text with a <%VAR%> to interpolate'; my $vars = { 'VAR' => 'value taken from a data structure' }; $_ = $text; while ( /<%(.+?)%>/g) { #print "$1\n"; if (!defined $vars->{$1} ) { $vars->{$1} = 'undefined'; $vars->{MESSAGE} .= "$1 undefined
\n"; ## keep a log of missing variables } } $text =~ s/<%(.+?)%>/$vars->{$1}/g || warn("unable to substitute $1"); print qq{$text\n};