exercism-perl5

Repository for my Perl 5 Exercism exercises
git clone git://git.samirparikh.com/exercism-perl5
Log | Files | Refs | README

WordCount.pm (534B) - raw


      1 package WordCount;
      2 use strict;
      3 use warnings;
      4 use Exporter qw<import>;
      5 our @EXPORT_OK = qw<count_words>;
      6 
      7 sub count_words {
      8   my ($sentence) = @_;
      9   my %word_count;
     10   $sentence =~ s/(.)/\L$1/gi; # force everything to lower case
     11 # [\w\d]+ matches 1 or more "word" or decimal characters
     12 # (?:'t)? matches optional apostrophe t
     13 # the `?:` means a non capturing set of parentheses
     14 # the `gi` at the end means global search and case insensitive
     15   $word_count{$_}++ foreach ($sentence =~ m/([\w\d]+(?:'t)?)/gi);
     16   return \%word_count;
     17 }
     18 
     19 1;