package WordCount; use strict; use warnings; use Exporter qw; our @EXPORT_OK = qw; sub count_words { my ($sentence) = @_; my %word_count; $sentence =~ s/(.)/\L$1/gi; # force everything to lower case # [\w\d]+ matches 1 or more "word" or decimal characters # (?:'t)? matches optional apostrophe t # the `?:` means a non capturing set of parentheses # the `gi` at the end means global search and case insensitive $word_count{$_}++ foreach ($sentence =~ m/([\w\d]+(?:'t)?)/gi); return \%word_count; } 1;