use warnings; use strict; my %words = (word => 1, word2 => 1, word3 =>1); my $temp = 'WORD2'; my $temp1 = 'word2'; $_ = 'here is word2'; s/$temp1/$words{$temp1} ? "$temp " : "[$temp] "/ex; print "$_\n"; __END__ here is WORD2