File Coverage

blib/lib/App/PigLatin.pm
Criterion Covered Total %
statement 39 43 90.7
branch 11 14 78.5
condition 1 3 33.3
subroutine 7 8 87.5
pod 0 4 0.0
total 58 72 80.5


line stmt bran cond sub pod time code
1 2     2   36078 use strict;
  2         6  
  2         82  
2 2     2   11 use warnings;
  2         3  
  2         99  
3             # ABSTRACT: Converts given English text into Pig Latin
4             # Delon Newman Copyright (C) 2007
5              
6             package App::PigLatin;
7 2     2   22 use Exporter 'import';
  2         4  
  2         1350  
8             our @EXPORT_OK = qw(translate);
9              
10             sub translate {
11 3     3 0 15 my $text = shift;
12 3   33     16 my $dict = shift || create_dictionary($$text);
13              
14 3         13 for (keys %$dict) { $$text =~ s/\b$_\b/$dict->{$_}/g }
  10         196  
15              
16 3         26 $$text;
17             }
18              
19             sub create_dictionary {
20 3     3 0 6 my $text = shift;
21              
22 3         9 $text =~ s/[\,\.\:\;\-\?\!\%\*\#\^\(\)\@\$\+="\[\]{}\\\|\'\/]/ /g;
23 3         20 my @words = split /[\s\n]+/, $text;
24 3         6 my %dict;
25              
26 3         8 for (@words) {
27 11 50       28 next if /\d/;
28 11 50       26 next unless $_;
29 11 100       26 next if $dict{$_};
30              
31 10 100       22 if (starts_with_vowl($_)) {
32 2         105 $dict{$_} = $_;
33             } else {
34             # FIXME: This should exclude y in
35             # cases where it is a vowl
36 8         25 m/([b-df-hj-np-tv-z]+)/i;
37 8         38 $dict{$_} = $_ . lc($1);
38 8         74 $dict{$_} =~ s/$1//i;
39             }
40              
41 10 50       41 if (/[A-Z]{2,}/) { $dict{$_} .= 'AY' }
  0         0  
42 10         22 else { $dict{$_} .= 'ay' }
43 10 100       44 $dict{$_} = ucfirst($dict{$_}) if (/^[A-Z]/);
44             }
45            
46 3         18 \%dict;
47             }
48              
49             sub _starts_with {
50 10     10   13 my $word = shift;
51 10         23 my @letters = @_;
52 10 100       95 map { return 1 if ($word =~ /^$_/i) } @letters;
  44         427  
53            
54 8         33 0;
55             }
56              
57             sub starts_with_vowl {
58 10     10 0 16 my $word = shift;
59 10         23 my @vowls = qw{a e i o u};
60            
61 10         24 _starts_with($word, @vowls);
62             }
63              
64             sub starts_with_consonant {
65 0     0 0   my $word = shift;
66 0           my @consonants = qw{b c d f g h j k l m n p q r s t v w x y z};
67            
68 0           _starts_with($word, @consonants);
69             }
70              
71             1;