my %hash = (...); my $src = do { local $/; <> }; my $res = join '', map $hash{$_} // $_, split /\b/, $src; # %hash = (a => 'xxx') # $src = 'a b ab'; # $res = 'xxx b ab';