File Coverage

blib/lib/Acme/Opish.pm
Criterion Covered Total %
statement 52 52 100.0
branch 16 18 88.8
condition n/a
subroutine 10 10 100.0
pod 3 3 100.0
total 81 83 97.5


line stmt bran cond sub pod time code
1             # $Id: Opish.pm,v 1.2 2003/09/28 08:50:37 gene Exp $
2              
3             package Acme::Opish;
4              
5 1     1   29918 use vars qw($VERSION);
  1         3  
  1         58  
6             $VERSION = '0.0601';
7              
8 1     1   6 use strict;
  1         1  
  1         37  
9 1     1   5 use Carp;
  1         6  
  1         101  
10 1     1   4 use base qw(Exporter);
  1         2  
  1         114  
11 1     1   5 use vars qw(@EXPORT @EXPORT_OK);
  1         1  
  1         55  
12             @EXPORT = @EXPORT_OK = qw(
13             enop
14             has_silent_e
15             no_silent_e
16             );
17 1     1   4 use File::Basename;
  1         1  
  1         1071  
18              
19             # no_silent_e list {{{
20             my %OK; @OK{qw(
21             adobe
22             acme
23             acne
24             anime
25             antistrophe
26             apostrophe
27             be
28             breve
29             Brule
30             cabriole
31             cache
32             Calliope
33             capote
34             Catananche
35             catastrophe
36             clave
37             cliche
38             consomme
39             coyote
40             diastrophe
41             epanastrophe
42             epitome
43             forte
44             Giuseppe
45             kamikaze
46             karate
47             me
48             misogyne
49             Pele
50             phlebotome
51             progne
52             Psyche
53             psyche
54             Quixote
55             recipie
56             Sade
57             Salome
58             saute
59             stanze
60             supercatastrophe
61             Tempe
62             tousche
63             tsetse
64             tonsillectome
65             tonsillotome
66             tracheotome
67             ukulele
68             we
69             zimbabwe
70             )} = undef;
71             # }}}
72              
73             # Add 'no_silent_e' entries if present and then return the list.
74             sub no_silent_e {
75 2     2 1 191 $OK{$_} = undef for @_;
76 2         9 return keys %OK;
77             }
78              
79             # Remove'no_silent_e' entries if present and then return the list.
80             sub has_silent_e {
81 1     1 1 5 delete $OK{$_} for @_;
82 1         4 return keys %OK;
83             }
84              
85             # Prefix vowels not declared in the 'no_silent_e' list.
86             sub enop {
87 11     11 1 2157 my $prefix = 'op';
88             # If present, the prefix is given as a named parameter.
89 11 100       31 if ($_[0] eq '-opish_prefix') {
90 1         2 shift;
91 1         2 $prefix = shift;
92             }
93              
94             # Process the given text stream.
95 11         20 my @strings = @_;
96             # Given as a known system filename.
97 11         22 for (@strings) { # {{{
98 14 100       122 if (-f) {
99             # Open the file for reading.
100 1 50       43 open IN, $_ or carp "Can't read $_: $!\n";
101              
102             # Construct a new filename.
103 1         66 my ($name, $path) = fileparse($_, '');
104 1         4 $_ = $path . 'opish-' . $name;
105              
106             # Open the new file for writing.
107 1 50       127 open OUT, ">$_" or carp "Can't write $_: $!\n";
108              
109             # Write opish to the file.
110 1         25 while (my $line = ) {
111 4         11 print OUT _to_opish($prefix, $line), "\n";
112             }
113              
114             # Close the files.
115 1         14 close IN;
116 1         64 close OUT;
117             } # }}}
118             # ..or given as strings on the commandline.
119             else {
120 13         27 $_ = _to_opish($prefix, $_);
121             }
122             }
123              
124 11         62 return @strings;
125             }
126              
127             # DrMath++ && DrForr++ && Yay!
128             sub _to_opish {
129 17     17   25 my ($prefix, $string) = @_;
130              
131             # XXX Oof. We don't preserve whitespace. : \
132 17         51 my @words = split /\s+/, $string;
133              
134             # Process each word as a unit.
135 17         28 for (@words) {
136             # Is this word capitalized?
137 35 100       86 my $is_capped = /^[A-Z]/ ? 1 : 0;
138             # Lowercase the first letter in case we have to prefix it.
139 35         55 $_ = lcfirst;
140              
141             # Okay. Prefix the sucka.
142             # XXX Ack. How can I simplify this ugliness?
143 35 100       131 if (exists $OK{ lc $_ }) { # {{{
    100          
    100          
144 2         11 s/
145             ( # Capture...
146             [aeiouy]+ # consecutive vowels
147             \B # that do not terminate at a word boundry
148             (?![aeiouy]) # that are not followed by another vowel
149             | # or
150             [aeiouy]* # any consecutive vowels
151             [aeiouy] # with any vowel following
152             \b # that terminates at a word boundry.
153             ) # ...end capture.
154             /$prefix$1/gisx; # Add 'op' to what we captured.
155             } # }}}
156             # Special case 'ye'.
157             elsif (lc ($_) eq 'ye') {
158 1         5 $_ = 'y' . $prefix . substr ($_, -1);
159             }
160             # We don't want to prefix a non-vowel y.
161             elsif (/^y[aeiouy]/i) { # {{{
162 1         13 s/
163             (?:^y)? # Our string starts with y, but we don't
164             # want to consider it for every match.
165             ( # Capture...
166             [aeiouy]+ # consecutive vowels
167             \B # that do not terminate at a word boundry
168             (?![aeiouy]) # that are not followed by another vowel
169             | # or
170             [aeiouy]* # any consecutive vowels
171             [aiouy] # with any non-e vowel following
172             \b # that terminates at a word boundry.
173             | # or
174             [aeiouy]+ # consecutive vowels
175             [aeiouy] # with any vowel following
176             \b # that terminates at a word boundry.
177             ) # ...end capture.
178             /$prefix$1/gisx; # Add 'op' to what we captured.
179              
180 1         3 $_ = 'y' . $_;
181             } # }}}
182             # This regexp captures the "non-solitary, trailing e" vowels.
183             else { # {{{
184 31         226 s/
185             ( # Capture...
186             [aeiouy]+ # consecutive vowels
187             \B # that do not terminate at a word boundry
188             (?![aeiouy]) # that are not followed by another vowel
189             | # or
190             [aeiouy]* # any consecutive vowels
191             [aiouy] # with any non-e vowel following
192             \b # that terminates at a word boundry.
193             | # or
194             [aeiouy]+ # consecutive vowels
195             [aeiouy] # with any vowel following
196             \b # that terminates at a word boundry.
197             ) # ...end capture.
198             /$prefix$1/gisx; # Add 'op' to what we captured.
199             } # }}}
200              
201             # The original word was capitalized.
202 35 100       113 $_ = ucfirst if $is_capped;
203             }
204              
205             # Return the words as a single space separated string.
206             # XXX Again, oof. We don't preserve whitespace. : \
207 17         100 return join ' ', @words;
208             }
209              
210             1;
211             __END__