use Modern::Perl; open my $IN, '<', './hamlet.txt' or die $!; my @wordlist = qw/prince denmark father hamlet be ghost/; my %words; while ( my $line = <$IN> ) { $words{ lc $_ }++ for split /\b/, $line; } say "$_: $words{$_}" for @wordlist;