my %hash = ( var1 => 'value1', var2 => 'value2' ); # ... or whatever... while ( $tmpl =~ /@~(.*?)~@/ and exists( $hash{$1} )) { my $var = $1; $tmpl =~ s/@~$var~@/$hash{$var}/g; } croak "Bad template text:\n$tmpl" if ( $tmpl =~ /@~|~@/ );