use strict; use warnings; use Data::Dumper; open my $dictFH, q{<}, \ <<'DICT' or die qq{open: < HEREDOC: $!\n}; 1 eat 2 habit 3 boy 4 man-kind 5 man 6 kind 7 bar 8 graph 9 bar graph DICT my %dict = reverse map { split m{\s+}, $_, 2 } map { chomp; $_ } <$dictFH>; close $dictFH or die qq{close: < HEREDOC: $!\n}; print Data::Dumper->Dumpxs( [ \ %dict ], [ qw{ *dict } ] ); my $text = <<'TEXT'; The boy has a habit of eating kind of like a man. Man-kind, as a group, does not wear habits. Kind of you to watch what you eat. Make a bar graph of what drinks are drunk in a bar and the graph could be coloured in. TEXT my $rxDict = do { local $" = q{|}; qr {(?xi) \b( @{ [ map quotemeta, sort { $b cmp $a } keys %dict ] } )\b } }; $text =~ s{$rxDict}{ $dict{ lc $1 } }eg; print $text;