blib/lib/Lingua/Conjunction.pm

filter: Statementstmt 86.7% 46 / 53 Branchbran 62.5% 15 / 24 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 76.9% 10 / 13 Podpod 100% 7 / 7 totaltotal 78.5% 84 / 107 SCAR 29.9
CC
17
Cov
78%
_get_language
9.5
conjunction
8.3
lang
3.3
CRAP 19.9
↑ Summary
1 package Lingua::Conjunction;
2  
3 # ABSTRACT: Convert lists into simple linguistic conjunctions
4  
5 3 ▶use v5.8;
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.4
23  
24 =cut
25  
26 3 our $VERSION = '2.4';
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 # (ie, "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 13 ▶ my @list = grep defined && /\S/, @_;
Subroutine conjunction: called CC=8 SCAR=21.2
Pod documented
Condition100%3/3
defined $_ && /\S/
!ll&&!rl&&r
1629
MC/DC100%2/2
defined $_ && /\S/
defined $_ /\S/
Truth table100%3/3
A && B
A= defined $_B= /\S/
ABresult
0X0
100
111
115  
116 13 ▶ return if(scalar(@list) == 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @list == 0 0 13 13
117 13 ▶ return $list[0] if(scalar(@list) == 1);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @list == 1 2 11 13
118 11 ▶ return join(" $punct{$list_type} ", @list) if(scalar(@list) == 2);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @list == 2 6 5 11
119  
120 11 ▶ if ( $punct{pen} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($punct{'pen'}) { } 3 2 5
121 3 ▶ return join "$punct{sep} ", @list[ 0 .. $#list - 1 ],
Branch: 1 branch
BranchTrueFalseTotal
unless grep /$punct{"sep"}/, @list 3 0 3
122 "$punct{$list_type} $list[-1]",
123 unless grep /$punct{sep}/, @list;
124 0 return join "$punct{alt} ", @list[ 0 .. $#list - 1 ],
125 "$punct{$list_type} $list[-1]";
126 } else {
127 8 ▶ 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 6 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 =cut
142  
143 sub separator {
144 0 ▶ $punct{sep} = $_[1];
Subroutine separator: not called CC=1 SCAR=6.9
Pod documented
145 }
146  
147 =head2 separator_phrase
148  
149 Sets the alternate (phrase) separator.
150  
151 Lingua::Conjunction->separator_phrase(';');
152  
153 The C<separator_phrase> is used whenever the separator already appears in
154 an item of the list. For example:
155  
156 # emits "Doe, a deer; Ray; and Me"
157 $name_list = conjunction('Doe, a deer', 'Ray', 'Me');
158  
159 =cut
160  
161 sub separator_phrase {
162 0 ▶ $punct{alt} = $_[1];
Subroutine separator_phrase: not called CC=1 SCAR=6.9
Pod documented
163 }
164  
165 =head2 penultimate
166  
167 Enables/disables penultimate separator.
168  
169 You may use the C<penultimate> routine to disable the separator after the
170 next to last item.
171 In English, The Oxford Comma is a highly debated issue.
172  
173 # emits "Jack, Jill and Spot"
174 Lingua::Conjunction->penultimate(0);
175 $name_list = conjunction('Jack', 'Jill', 'Spot');
176  
177 The original author was told that the penultimate comma is not standard for some
178 languages, such as Norwegian.
179 Hence the defaults set in the C<%languages>.
180  
181 Lingua::Conjunction->penultimate(0);
182  
183 =cut
184  
185 sub penultimate {
186 1 ▶ $punct{pen} = $_[1];
Subroutine penultimate: called CC=1 SCAR=0.0
Pod documented
187 }
188  
189 =head2 connector_type
190  
191 Use "and" or "or", with appropriate translation for the current language
192  
193 Lingua::Conjunction->connector_type('and');
194  
195 =cut
196  
197 sub connector_type {
198 1 ▶ croak "Undefined connector type \`$_[1]\'", unless ( $types{ $_[1] } );
Subroutine connector_type: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $types{$_[1]} 0 1 1
199 1 $list_type = $types{ $_[1] };
200 }
201  
202 =head2 connector
203  
204 Sets the for the current connector_type.
205  
206 Lingua::Conjunction->connector(SCALAR)
207  
208 =cut
209  
210 sub connector {
211 0 ▶ $punct{$list_type} = $_[1];
Subroutine connector: not called CC=1 SCAR=6.9
Pod documented
212 }
213  
214 =head2 lang
215  
216 Sets the language to use.
217 If no arguments are given,
218 it tries its best to guess.
219  
220 Lingua::Conjunction->lang('de'); # Changes the language to German
221  
222 =cut
223  
224 sub lang {
225 1 ▶ my $language = $_[1] || _get_language();
Subroutine lang: called CC=3 SCAR=12.0
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
226 croak "Undefined language \`$language\'",
227 1 ▶ unless ( defined( $language{$language} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $language{$language} 0 1 1
228 1 %punct = %{ $language{$language} };
229 }
230  
231 # https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html
232 # https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html
233 sub _get_language
234 {
235 3 ▶ if($ENV{'LANGUAGE'}) {
Subroutine _get_language: called CC=7 SCAR=22.5
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LANGUAGE"}) 2 1 3
236 2 foreach my $l(split/:/, $ENV{'LANGUAGE'}) {
237 2 ▶ if($language{$l}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($language{$l}) 2 0 2
238 2 return $l;
239 }
240 }
241 }
242 1 foreach my $variable('LC_ALL', 'LC_MESSAGES', 'LANG') {
243 3 my $val = $ENV{$variable};
244 3 ▶ next unless(defined($val));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 3 0 3
245  
246 0 $val = substr($val, 0, 2);
247 0 ▶ if($language{$val}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($language{$val}) 0 0 0
248 0 return $val;
249 }
250 }
251 1 return 'en';
252 }
253  
254 =head1 AUTHORS
255  
256 =over 4
257  
258 =item *
259  
260 Robert Rothenberg <rrwo@cpan.org>
261  
262 =item *
263  
264 Damian Conway <damian@conway.org>
265  
266 =back
267  
268 =head1 MAINTAINER
269  
270 2021-present Maintained by Nigel Horne, C<< <njh at bandsman.co.uk> >>
271  
272 =head1 CONTRIBUTORS
273  
274 =for stopwords Ade Ishs Mohammad S Anwar Nigel Horne
275  
276 =over 4
277  
278 =item *
279  
280 Ade Ishs <adeishs@cpan.org>
281  
282 =item *
283  
284 Mohammad S Anwar <mohammad.anwar@yahoo.com>
285  
286 =item *
287  
288 Nigel Horne C<< <njh at bandsman.co.uk> >>
289  
290 =back
291  
292 =head1 SEE ALSO
293  
294 C<Locale::Language>, C<List::ToHumanString>
295  
296 The I<Perl Cookbook> in Section 4.2 has a similar subroutine called
297 C<commify_series>. The differences are that
298 1. this routine handles multiple languages and
299 2. being a module, you do not have to add the subroutine to a script every time you need it.
300  
301 =head1 SOURCE
302  
303 The development version is on github at L<https://github.com/nigelhorne/Lingua-Conjunction>
304 and may be cloned from L<git://github.com/nigelhorne/Lingua-Conjunction.git>
305  
306 =head1 SUPPORT
307  
308 You can find documentation for this module with the perldoc command.
309  
310 perldoc Lingua::Conjunction
311  
312 You can also look for information at:
313  
314 =over 4
315  
316 =item * MetaCPAN
317  
318 L<https://metacpan.org/release/Lingua-Conjunction>
319  
320 =item * RT: CPAN's request tracker
321  
322 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Lingua-Conjunction>
323  
324 =item * CPANTS
325  
326 L<http://cpants.cpanauthors.org/dist/Lingua-Conjunction>
327  
328 =item * CPAN Testers' Matrix
329  
330 L<http://matrix.cpantesters.org/?dist=Lingua-Conjunction>
331  
332 =item * CPAN Ratings
333  
334 L<http://cpanratings.perl.org/d/Lingua-Conjunction>
335  
336 =item * CPAN Testers Dependencies
337  
338 L<http://deps.cpantesters.org/?module=Lingua::Conjunction>
339  
340 =back
341  
342 =head1 BUGS
343  
344 Please report any bugs or feature requests on the bugtracker website
345 L<https://rt.cpan.org/Dist/Display.html?Queue=Lingua-Conjunction>
346  
347 When submitting a bug or request, please include a test-file or a
348 patch to an existing test-file that illustrates the bug or desired
349 feature.
350  
351 =head1 COPYRIGHT AND LICENSE
352  
353 This software is Copyright (c) 1999-2020 by Robert Rothenberg.
354  
355 This is free software, licensed under:
356  
357 The Artistic License 2.0 (GPL Compatible)
358  
359 =cut
360  
361 3 1;
↑ Summary