blib/lib/Lingua/Conjunction.pm

filter: Statementstmt 76.9% 50 / 65 Branchbran 66.6% 16 / 24 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 76.9% 10 / 13 Podpod 100% 7 / 7 totaltotal 74.7% 89 / 119 CC 17 SCAR 16.9
CC
17
Cov
74%
_get_language
9.5
conjunction
8.1
lang
3.4
CRAP 5.4
↑ Summary
1 package Lingua::Conjunction;
2  
3 # ABSTRACT: Convert lists into simple linguistic conjunctions
4  
5 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Exporter qw/ import /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our @EXPORT = qw( conjunction );
14 3 our @EXPORT_OK = @EXPORT;
15  
16 =head1 NAME
17  
18 Lingua::Conjunction - Convert lists into simple linguistic conjunctions
19  
20 =head1 VERSION
21  
22 Version 2.5
23  
24 =cut
25  
26 3 our $VERSION = '2.5';
27  
28 =head1 SYNOPSIS
29  
30 Language-specific definitions.
31 These may not be correct, and certainly they are not complete.
32 E-mail corrections and additions to C<< <njh at bandsman.co.uk> >>,
33 and an updated version will be released.
34  
35 =cut
36  
37 # Format of %language is as follows:
38 # Two-letter ISO language codes... see L<Locale::Language> from CPAN for more details.
39 # sep = item separator (usually a comma)
40 # alt = alternate ("phrase") separator
41 # pen = 1 = use penultimate separator/0 = don't use penultimate
42 # (i.e., "Jack, Jill and Spot" vs. "Jack, Jill, and Spot")
43 # con = conjunction ("and")
44 # dis = disjunction ("or"), well, grammatically still a "conjunction"...
45  
46 3 my %language = (
47 'af' => { sep => ',', alt => ';', pen => 1, con => 'en', dis => 'of' },
48 'da' => { sep => ',', alt => ';', pen => 1, con => 'og', dis => 'eller' },
49 'de' => { sep => ',', alt => ';', pen => 1, con => 'und', dis => 'oder' },
50 'en' => { sep => ',', alt => ';', pen => 1, con => 'and', dis => 'or' },
51 'es' => { sep => ',', alt => ';', pen => 1, con => 'y', dis => 'o' },
52 'fi' => { sep => ',', alt => ';', pen => 1, con => 'ja', dis => 'tai' },
53 'fr' => { sep => ',', alt => ';', pen => 0, con => 'et', dis => 'ou' },
54 'id' => { sep => ',', alt => ';', pen => 1, con => 'dan', dis => 'atau' },
55 'it' => { sep => ',', alt => ';', pen => 1, con => 'e', dis => 'o' },
56 'la' => { sep => ',', alt => ';', pen => 1, con => 'et', dis => 'vel' },
57 'nl' => { sep => ',', alt => ';', pen => 1, con => 'en', dis => 'of' },
58 'no' => { sep => ',', alt => ';', pen => 0, con => 'og', dis => 'eller' },
59 'pt' => { sep => ',', alt => ';', pen => 1, con => 'e', dis => 'ou' },
60 'sw' => { sep => ',', alt => ';', pen => 1, con => 'na', dis => 'au' },
61 );
62  
63 # Conjunction types. TODO: Someday we'll add either..or, neither..nor
64 3 my %types = (
65 'and' => 'con',
66 'or' => 'dis'
67 );
68  
69 3 my %punct = %{ $language{_get_language()} };
70 3 my $list_type = $types{'and'};
71  
72 =head1 SUBROUTINES/METHODS
73  
74 =head2 conjunction
75  
76 Lingua::Conjunction exports a single subroutine, C<conjunction>, that
77 converts a list into a properly punctuated text string.
78  
79 You can cause C<conjunction> to use the connectives of other languages, by
80 calling the appropriate subroutine:
81  
82 Lingua::Conjunction->lang('en'); # use 'and'
83 Lingua::Conjunction->lang('es'); # use 'y'
84 Lingua::Conjunction->lang(); # Tries to determine your language, otherwise falls back to 'en'
85  
86 Supported languages in this version are
87 Afrikaans,
88 Danish,
89 Dutch,
90 English,
91 French,
92 German,
93 Indonesian,
94 Italian,
95 Latin,
96 Norwegian,
97 Portuguese,
98 Spanish,
99 and Swahili.
100  
101 You can also set connectives individually:
102  
103 Lingua::Conjunction->separator("...");
104 Lingua::Conjunction->separator_phrase("--");
105 Lingua::Conjunction->connector_type("or");
106  
107 # emits "Jack... Jill... or Spot"
108 $name_list = conjunction('Jack', 'Jill', 'Spot');
109  
110 =cut
111  
112 sub conjunction {
113 # See List::ToHumanString
114 17 ▶ my @list = grep defined && /\S/, @_;
Subroutine conjunction: called CC=8 SCAR=20.9
Pod documented
Condition100%3/3
defined $_ && /\S/
!ll&&!rl&&r
21038
MC/DC100%2/2
defined $_ && /\S/
defined $_ /\S/
Truth table100%3/3
A && B
A= defined $_B= /\S/
ABresult
0X0
100
111
115  
116 17 ▶ return if(scalar(@list) == 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @list == 0 0 17 17
117 17 ▶ return $list[0] if(scalar(@list) == 1);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @list == 1 2 15 17
118 15 ▶ return join(" $punct{$list_type} ", @list) if(scalar(@list) == 2);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @list == 2 9 6 15
119  
120 6 ▶ if ( $punct{pen} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($punct{'pen'}) { } 4 2 6
121 4 ▶ return join "$punct{sep} ", @list[ 0 .. $#list - 1 ],
Branch: 1 branch
BranchTrueFalseTotal
unless grep /$punct{"sep"}/, @list 3 1 4
122 "$punct{$list_type} $list[-1]",
123 unless grep /$punct{sep}/, @list;
124 1 return join "$punct{alt} ", @list[ 0 .. $#list - 1 ],
125 "$punct{$list_type} $list[-1]";
126 } else {
127 2 ▶ return join "$punct{sep} ", @list[ 0 .. $#list - 2 ],
Branch: 1 branch
BranchTrueFalseTotal
unless grep /$punct{"sep"}/, @list 2 0 2
128 "$list[-2] $punct{$list_type} $list[-1]",
129 unless grep /$punct{sep}/, @list;
130 0 return join "$punct{alt} ", @list[ 0 .. $#list - 2 ],
131 "$list[-2] $punct{$list_type} $list[-1]";
132 }
133 }
134  
135 =head2 separator
136  
137 Sets the separator, usually ',' or ';'.
138  
139 Lingua::Conjunction->separator(',');
140  
141 Returns the previous value.
142  
143 =cut
144  
145 sub separator {
146 0 ▶ my $rc = $punct{'sep'};
Subroutine separator: not called CC=1 SCAR=6.9
Pod documented
147  
148 0 $punct{sep} = $_[1];
149 0 return $rc;
150 }
151  
152 =head2 separator_phrase
153  
154 Sets the alternate (phrase) separator.
155  
156 Lingua::Conjunction->separator_phrase(';');
157  
158 The C<separator_phrase> is used whenever the separator already appears in
159 an item of the list. For example:
160  
161 # emits "Doe, a deer; Ray; and Me"
162 $name_list = conjunction('Doe, a deer', 'Ray', 'Me');
163  
164 Returns the previous value;
165  
166 =cut
167  
168 sub separator_phrase {
169 0 ▶ my $rc = $punct{'alt'};
Subroutine separator_phrase: not called CC=1 SCAR=6.9
Pod documented
170  
171 0 $punct{alt} = $_[1];
172 0 return $rc;
173 }
174  
175 =head2 penultimate
176  
177 Enables/disables penultimate separator.
178  
179 You may use the C<penultimate> routine to disable the separator after the
180 next to last item.
181 In English, The Oxford Comma is a highly debated issue.
182  
183 # emits "Jack, Jill and Spot"
184 Lingua::Conjunction->penultimate(0);
185 $name_list = conjunction('Jack', 'Jill', 'Spot');
186  
187 The original author was told that the penultimate comma is not standard for some
188 languages, such as Norwegian.
189 Hence the defaults set in the C<%languages>.
190  
191 Lingua::Conjunction->penultimate(0);
192  
193 Returns the previous value.
194  
195 =cut
196  
197 sub penultimate {
198 1 ▶ my $rc = $punct{'pen'};
Subroutine penultimate: called CC=1 SCAR=0.0
Pod documented
199  
200 1 $punct{pen} = $_[1];
201 1 return $rc;
202 }
203  
204 =head2 connector_type
205  
206 Use "and" or "or", with appropriate translation for the current language
207  
208 Lingua::Conjunction->connector_type('and');
209  
210 =cut
211  
212 sub connector_type {
213 1 ▶ if($types{ $_[1]}) {
Subroutine connector_type: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ($types{$_[1]}) { } 1 0 1
214 1 $list_type = $types{ $_[1] };
215 } else {
216 0 croak "Undefined connector type \`$_[1]\'"
217 }
218  
219 1 return $list_type;
220 }
221  
222 =head2 connector
223  
224 Sets the for the current connector_type.
225  
226 Lingua::Conjunction->connector(SCALAR)
227  
228 Returns the previous value.
229  
230 =cut
231  
232 sub connector {
233 0 ▶ my $rc = $punct{'list_type'};
Subroutine connector: not called CC=1 SCAR=6.9
Pod documented
234  
235 0 $punct{$list_type} = $_[1];
236 0 return $rc;
237 }
238  
239 =head2 lang
240  
241 Sets the language to use.
242 If no arguments are given,
243 it tries its best to guess.
244  
245 Lingua::Conjunction->lang('de'); # Changes the language to German
246  
247 =cut
248  
249 sub lang {
250 1 ▶ my $language = $_[1] || _get_language();
Subroutine lang: called CC=3 SCAR=12.3
Pod documented
Condition33%1/3
$_[1] || _get_language()
l!l&&r!l&&!r
100
MC/DC0%0/2
$_[1] || _get_language()
$_[1] _get_language()
Truth table33%1/3
A || B
A= $_[1]B= _get_language()
ABresult
1X1
011
000
251  
252 1 ▶ if(defined($language{$language})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $language{$language}) { } 1 0 1
253 1 %punct = %{ $language{$language} };
254 } else {
255 0 croak "Undefined language \`$language\'";
256 }
257  
258 1 return $language;
259 }
260  
261 # https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html
262 # https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html
263 sub _get_language
264 {
265 3 ▶ if($ENV{'LANGUAGE'}) {
Subroutine _get_language: called CC=7 SCAR=22.5
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LANGUAGE"}) 2 1 3
266 2 foreach my $l(split/:/, $ENV{'LANGUAGE'}) {
267 2 ▶ if($language{$l}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($language{$l}) 2 0 2
268 2 return $l;
269 }
270 }
271 }
272 1 foreach my $variable('LC_ALL', 'LC_MESSAGES', 'LANG') {
273 3 my $val = $ENV{$variable};
274 3 ▶ next unless(defined($val));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 3 0 3
275  
276 0 $val = substr($val, 0, 2);
277 0 ▶ if($language{$val}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($language{$val}) 0 0 0
278 0 return $val;
279 }
280 }
281 1 return 'en';
282 }
283  
284 =head1 AUTHORS
285  
286 =over 4
287  
288 =item *
289  
290 Robert Rothenberg <rrwo@cpan.org>
291  
292 =item *
293  
294 Damian Conway <damian@conway.org>
295  
296 =back
297  
298 =head1 MAINTAINER
299  
300 2021-present Maintained by Nigel Horne, C<< <njh at bandsman.co.uk> >>
301  
302 =head1 CONTRIBUTORS
303  
304 =for stopwords Ade Ishs Mohammad S Anwar Nigel Horne
305  
306 =over 4
307  
308 =item *
309  
310 Ade Ishs <adeishs@cpan.org>
311  
312 =item *
313  
314 Mohammad S Anwar <mohammad.anwar@yahoo.com>
315  
316 =item *
317  
318 Nigel Horne C<< <njh at bandsman.co.uk> >>
319  
320 =back
321  
322 =head1 SEE ALSO
323  
324 C<Locale::Language>, C<List::ToHumanString>
325  
326 The I<Perl Cookbook> in Section 4.2 has a similar subroutine called
327 C<commify_series>. The differences are that
328 1. this routine handles multiple languages and
329 2. being a module, you do not have to add the subroutine to a script every time you need it.
330  
331 =head1 SOURCE
332  
333 The development version is on github at L<https://github.com/nigelhorne/Lingua-Conjunction>
334 and may be cloned from L<git://github.com/nigelhorne/Lingua-Conjunction.git>
335  
336 =head1 SUPPORT
337  
338 You can find documentation for this module with the perldoc command.
339  
340 perldoc Lingua::Conjunction
341  
342 You can also look for information at:
343  
344 =over 4
345  
346 =item * MetaCPAN
347  
348 L<https://metacpan.org/release/Lingua-Conjunction>
349  
350 =item * RT: CPAN's request tracker
351  
352 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Lingua-Conjunction>
353  
354 =item * CPANTS
355  
356 L<http://cpants.cpanauthors.org/dist/Lingua-Conjunction>
357  
358 =item * CPAN Testers' Matrix
359  
360 L<http://matrix.cpantesters.org/?dist=Lingua-Conjunction>
361  
362 =item * CPAN Ratings
363  
364 L<http://cpanratings.perl.org/d/Lingua-Conjunction>
365  
366 =item * CPAN Testers Dependencies
367  
368 L<http://deps.cpantesters.org/?module=Lingua::Conjunction>
369  
370 =back
371  
372 =head1 BUGS AND LIMITATIONS
373  
374 Please report any bugs or feature requests on the bugtracker website
375 L<https://rt.cpan.org/Dist/Display.html?Queue=Lingua-Conjunction>
376  
377 When submitting a bug or request, please include a test-file or a
378 patch to an existing test-file that illustrates the bug or desired
379 feature.
380  
381 =head1 LICENSE AND COPYRIGHT
382  
383 This software is Copyright (c) 1999-2020 by Robert Rothenberg.
384  
385 This is free software, licensed under:
386  
387 The Artistic License 2.0 (GPL Compatible)
388  
389 =cut
390  
391 3 1;
↑ Summary