lib/PersonName/Format/Pattern.pm

filter: Statementstmt 90.3% 348 / 385 Branchbran 79.2% 130 / 164 Conditioncond 68.4% 63 / 92 MC/DCmcdc 37.0% 20 / 54 Subroutinesub 88.1% 37 / 42 Podpod 77.7% 7 / 9 totaltotal 81.1% 605 / 746 CC 127 SCAR 23.3
CC
127
Cov
84%
__ANON__
20.0
select_best
18.3
format_to_parts
15.4
CRAP 10.3
« lib/PersonName/Format/FieldModifier.pm ↑ Summary lib/PersonName/Format/Exception.pm »
1 ##----------------------------------------------------------------------------
2 ## Person Name Format - ~/lib/PersonName/Format/Pattern.pm
3 ## Version v0.1.0
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2026/07/16
7 ## Modified 2026/07/16
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package PersonName::Format::Pattern;
14 BEGIN
15 19 ▶{
Subroutine BEGIN: called CC=3 SCAR=13.2
16 19 ▶ use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 19 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 19 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 19 ▶ if( $] < 5.013 )
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.013") 0 19 19
20 {
21 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 0 ▶ unless( defined( &warnings::register_categories ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &warnings::register_categories) 0 0 0
23 {
24 *warnings::_mkMask = sub
25 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 0 my $bit = shift( @_ );
27 0 my $mask = "";
28 0 vec( $mask, $bit, 1 ) = 1;
29 0 return( $mask );
30 0 };
31  
32 *warnings::register_categories = sub
33 0 ▶ {
Subroutine __ANON__: not called CC=4 SCAR=30.0
34 0 my @names = @_;
35 0 foreach my $name ( @names )
36 {
37 0 ▶ if( !defined( $warnings::Bits{ $name } ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $warnings::Bits{$name}) 0 0 0
38 {
39 0 $warnings::Offsets{ $name } = $warnings::LAST_BIT;
40 0 $warnings::Bits{ $name } = warnings::_mkMask( $warnings::LAST_BIT++ );
41 0 $warnings::DeadBits{ $name } = warnings::_mkMask( $warnings::LAST_BIT++ );
42 0 ▶ if( length( $warnings::Bits{ $name } ) > length( $warnings::Bits{all} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $warnings::Bits{$name} > length $warnings::Bits{"all"}) 0 0 0
43 {
44 0 $warnings::Bits{all} .= "\x55";
45 0 $warnings::DeadBits{all} .= "\xaa";
46 }
47 }
48 }
49 0 };
50 }
51 }
52 19 warnings::register_categories( 'PersonName::Format' );
53 19 ▶ use parent qw( PersonName::Format::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
54 19 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
55 19 ▶ use PersonName::Format::FieldModifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 19 ▶ use PersonName::Format::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 19 ▶ use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
58 19 our( $VERSION ) = 'v0.1.0';
59 };
60  
61 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 19 my $FIELDS = {};
65 19 @$FIELDS{ qw(
66 title
67 given
68 given2
69 surname
70 surname2
71 generation
72 credentials
73 ) } = (1) x 7;
74  
75 19 my $MODIFIERS = {};
76 19 @$MODIFIERS{ qw(
77 informal
78 prefix
79 core
80 initial
81 monogram
82 allCaps
83 initialCap
84 retain
85 genitive
86 vocative
87 ) } = (1) x 10;
88  
89 sub init
90 57 ▶{
Subroutine init: called CC=6 SCAR=19.5
Pod documented
91 57 my $self = shift( @_ );
92 57 my $pattern = shift( @_ );
93 57 ▶ if( !defined( $pattern ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pattern) 0 57 57
94 {
95 0 return( $self->error( "No person-name pattern was provided." ) );
96 }
97 57 ▶ if( ref( $pattern ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pattern) 0 57 57
98 {
99 0 return( $self->error( "A person-name pattern must be a scalar value." ) );
100 }
101 57 my $args = $self->_get_args_as_hash( @_ );
102 # $self->{_init_strict_use_sub} = 1;
103 $self->SUPER::init(
104 debug => delete( $args->{debug} ),
105 57 ▶ fatal => delete( $args->{fatal} ),
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init("debug", delete $args->{"debug"}, "fatal", delete $args->{"fatal"}) or return $self->pass_error 57 0 57
106 ) || return( $self->pass_error );
107 57 ▶ if( scalar( keys( %$args ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$args) 0 57 57
108 {
109 0 return( $self->error( "Unknown Pattern option '", ( keys( %$args ) )[0], "'." ) );
110 }
111 57 $self->{pattern} = "$pattern";
112 57 ▶ $self->{tokens} = $self->_parse( "$pattern" ) ||
Condition100%2/2
$self->_parse("$pattern") || (return $self->pass_error)
l!l
4314
MC/DC100%1/1
$self->_parse("$pattern") || (return $self->pass_error)
$self->_parse("$pattern")
Truth table100%2/2
A
A= $self->_parse("$pattern")
Aresult
11
00
113 return( $self->pass_error );
114 43 return( $self );
115 }
116  
117 sub format
118 35 ▶{
Subroutine format: called CC=15 SCAR=27.2
Pod documented
119 35 my $self = shift( @_ );
120 35 my $name = shift( @_ );
121 35 my $args = $self->_get_args_as_hash( @_ );
122 35 ▶ unless( Scalar::Util::blessed( $name ) &&
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($name) and "PersonName::Format::Name"->implements_name_contract($name)) 0 35 35
Condition33%1/3
builtin::blessed($name) and "PersonName::Format::Name"->implements_name_contract($name)
!ll&&!rl&&r
0035
MC/DC0%0/2
builtin::blessed($name) and "PersonName::Format::Name"->implements_name_contract($name)
builtin::blessed($name) "PersonName::Format::Name"->implements_name_contract($name)
Truth table33%1/3
A and B
A= builtin::blessed($name)B= "PersonName::Format::Name"->implements_name_contract($name)
ABresult
0X0
100
111
123 PersonName::Format::Name->implements_name_contract( $name ) )
124 {
125 0 return( $self->error( "No name object was provided to format()." ) );
126 }
127 35 my $resolver = delete( $args->{modifier_resolver} );
128 35 ▶ if( scalar( keys( %$args ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$args) 0 35 35
129 {
130 0 return( $self->error( "Unknown format option '", ( keys( %$args ) )[0], "'." ) );
131 }
132 35 $name = $self->_prepare_name( $name );
133  
134 35 my $result = '';
135 35 my $seen_leading_field = 0;
136 35 my $seen_empty_leading_field = 0;
137 35 my $seen_empty_field = 0;
138 35 my $text_before = '';
139 35 my $text_after = '';
140  
141 35 foreach my $token ( @{$self->{tokens}} )
142 {
143 213 ▶ if( $token->{type} eq 'literal' )
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} eq "literal") 85 128 213
144 {
145 85 ▶ if( $seen_empty_leading_field )
Branch: 2 branches
BranchTrueFalseTotal
if ($seen_empty_leading_field) { } 10 75 85
elsif ($seen_empty_field) { } 27 48 75
146 {
147 10 next;
148 }
149 elsif( $seen_empty_field )
150 {
151 27 $text_after .= $token->{value};
152 }
153 else
154 {
155 48 $text_before .= $token->{value};
156 }
157 75 next;
158 }
159  
160 128 my $field_text = $self->_field_value( $name, $token, $resolver );
161 128 ▶ return( $self->pass_error ) if( !defined( $field_text ) && $self->error );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $field_text and $self->error 1 127 128
Condition100%3/3
not defined $field_text and $self->error
!ll&&!rl&&r
68591
MC/DC100%2/2
not defined $field_text and $self->error
not defined $field_text $self->error
Truth table100%3/3
not(A) and B
A= not defined $field_textB= $self->error
ABresult
0X0
100
111
162 127 ▶ if( !defined( $field_text ) ||
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $field_text && length $field_text) { } 59 68 127
Condition66%2/3
defined $field_text && length $field_text
!ll&&!rl&&r
59068
MC/DC50%1/2
defined $field_text && length $field_text
defined $field_text length $field_text
Truth table66%2/3
A && B
A= defined $field_textB= length $field_text
ABresult
0X0
100
111
163 !length( $field_text ) )
164 {
165 59 ▶ if( !$seen_leading_field )
Branch: 1 branch
BranchTrueFalseTotal
if (not $seen_leading_field) { } 11 48 59
166 {
167 11 $seen_empty_leading_field = 1;
168 11 $text_before = '';
169 }
170 else
171 {
172 48 $seen_empty_field = 1;
173 48 $text_after = '';
174 }
175 }
176 else
177 {
178 68 $seen_leading_field = 1;
179 68 $seen_empty_leading_field = 0;
180 68 ▶ if( $seen_empty_field )
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_empty_field) { } 16 52 68
181 {
182 16 $result .= $self->_coalesce( $text_before, $text_after );
183 16 $text_before = '';
184 16 $text_after = '';
185 16 $result .= $field_text;
186 16 $seen_empty_field = 0;
187 }
188 else
189 {
190 52 $result .= $text_before;
191 52 $text_before = '';
192 52 $result .= $field_text;
193 }
194 }
195 }
196 34 ▶ $result .= $text_before unless( $seen_empty_field );
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_empty_field 15 19 34
197 34 return( $result );
198 }
199  
200 sub format_to_parts
201 4 ▶{
Subroutine format_to_parts: called CC=15 SCAR=27.3
Pod undocumented
202 4 my $self = shift( @_ );
203 4 my $name = shift( @_ );
204 4 my $args = $self->_get_args_as_hash( @_ );
205 4 ▶ unless( Scalar::Util::blessed( $name ) &&
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($name) and "PersonName::Format::Name"->implements_name_contract($name)) 0 4 4
Condition33%1/3
builtin::blessed($name) and "PersonName::Format::Name"->implements_name_contract($name)
!ll&&!rl&&r
004
MC/DC0%0/2
builtin::blessed($name) and "PersonName::Format::Name"->implements_name_contract($name)
builtin::blessed($name) "PersonName::Format::Name"->implements_name_contract($name)
Truth table33%1/3
A and B
A= builtin::blessed($name)B= "PersonName::Format::Name"->implements_name_contract($name)
ABresult
0X0
100
111
206 PersonName::Format::Name->implements_name_contract( $name ) )
207 {
208 0 return( $self->error( "No name object was provided to format_to_parts()." ) );
209 }
210 4 my $resolver = delete( $args->{modifier_resolver} );
211 4 ▶ if( scalar( keys( %$args ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$args) 0 4 4
212 {
213 0 return( $self->error( "Unknown format_to_parts option '", ( keys( %$args ) )[0], "'." ) );
214 }
215 4 $name = $self->_prepare_name( $name );
216  
217 4 my $parts = [];
218 4 my $seen_leading_field = 0;
219 4 my $seen_empty_leading_field = 0;
220 4 my $seen_empty_field = 0;
221 4 my $text_before = '';
222 4 my $text_after = '';
223  
224 4 foreach my $token ( @{$self->{tokens}} )
225 {
226 24 ▶ if( $token->{type} eq 'literal' )
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} eq "literal") 10 14 24
227 {
228 10 ▶ if( $seen_empty_leading_field )
Branch: 2 branches
BranchTrueFalseTotal
if ($seen_empty_leading_field) { } 2 8 10
elsif ($seen_empty_field) { } 3 5 8
229 {
230 2 next;
231 }
232 elsif( $seen_empty_field )
233 {
234 3 $text_after .= $token->{value};
235 }
236 else
237 {
238 5 $text_before .= $token->{value};
239 }
240 8 next;
241 }
242  
243 14 my $field_text = $self->_field_value( $name, $token, $resolver );
244 14 ▶ return( $self->pass_error ) if( !defined( $field_text ) && $self->error );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $field_text and $self->error 0 14 14
Condition66%2/3
not defined $field_text and $self->error
!ll&&!rl&&r
860
MC/DC0%0/2
not defined $field_text and $self->error
not defined $field_text $self->error
Truth table66%2/3
not(A) and B
A= not defined $field_textB= $self->error
ABresult
0X0
100
111
245  
246 14 ▶ if( !defined( $field_text ) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $field_text and length $field_text) 6 8 14
Condition66%2/3
defined $field_text and length $field_text
!ll&&!rl&&r
608
MC/DC50%1/2
defined $field_text and length $field_text
defined $field_text length $field_text
Truth table66%2/3
A and B
A= defined $field_textB= length $field_text
ABresult
0X0
100
111
247 !length( $field_text ) )
248 {
249 6 ▶ if( !$seen_leading_field )
Branch: 1 branch
BranchTrueFalseTotal
if (not $seen_leading_field) { } 2 4 6
250 {
251 2 $seen_empty_leading_field = 1;
252 2 $text_before = '';
253 }
254 else
255 {
256 4 $seen_empty_field = 1;
257 4 $text_after = '';
258 }
259 6 next;
260 }
261  
262 8 $seen_leading_field = 1;
263 8 $seen_empty_leading_field = 0;
264  
265 8 ▶ if( $seen_empty_field )
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_empty_field) { } 3 5 8
266 {
267 3 my $literal = $self->_coalesce( $text_before, $text_after );
268 3 $self->_append_part( $parts, 'literal', $literal );
269 3 $text_before = '';
270 3 $text_after = '';
271 3 $seen_empty_field = 0;
272 }
273 else
274 {
275 5 $self->_append_part( $parts, 'literal', $text_before );
276 5 $text_before = '';
277 }
278  
279 $self->_append_part(
280 $parts,
281 $token->{source},
282 8 $field_text,
283 $token,
284 );
285 }
286  
287 4 ▶ unless( $seen_empty_field )
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen_empty_field) 3 1 4
288 {
289 3 $self->_append_part( $parts, 'literal', $text_before );
290 }
291 4 return( $parts );
292 }
293  
294 sub formatToParts
295 1 ▶{
Subroutine formatToParts: called CC=1 SCAR=0.0
Pod undocumented
296 1 return( shift->format_to_parts( @_ ) );
297 }
298  
299 sub num_empty_fields
300 40 ▶{
Subroutine num_empty_fields: called CC=10 SCAR=24.5
Pod documented
301 40 my $self = shift( @_ );
302 40 my $name = shift( @_ );
303 40 my $args = $self->_get_args_as_hash( @_ );
304 40 my $resolver = delete( $args->{modifier_resolver} );
305 40 ▶ if( scalar( keys( %$args ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$args) 0 40 40
306 {
307 0 return( $self->error( "Unknown num_empty_fields option '", ( keys( %$args ) )[0], "'." ) );
308 }
309 40 ▶ unless( Scalar::Util::blessed( $name ) &&
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($name) and "PersonName::Format::Name"->implements_name_contract($name)) 0 40 40
Condition33%1/3
builtin::blessed($name) and "PersonName::Format::Name"->implements_name_contract($name)
!ll&&!rl&&r
0040
MC/DC0%0/2
builtin::blessed($name) and "PersonName::Format::Name"->implements_name_contract($name)
builtin::blessed($name) "PersonName::Format::Name"->implements_name_contract($name)
Truth table33%1/3
A and B
A= builtin::blessed($name)B= "PersonName::Format::Name"->implements_name_contract($name)
ABresult
0X0
100
111
310 PersonName::Format::Name->implements_name_contract( $name ) )
311 {
312 0 return( $self->error( "No valid name object was provided." ) );
313 }
314 40 $name = $self->_prepare_name( $name );
315 40 my $count = 0;
316 40 foreach my $token ( @{$self->{tokens}} )
317 {
318 288 ▶ next unless( $token->{type} eq 'field' );
Branch: 1 branch
BranchTrueFalseTotal
unless $token->{"type"} eq "field" 119 169 288
319 169 my $value = $self->_field_value( $name, $token, $resolver );
320 169 ▶ return( $self->pass_error ) if( !defined( $value ) && $self->error );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value and $self->error 0 169 169
Condition66%2/3
not defined $value and $self->error
!ll&&!rl&&r
83860
MC/DC0%0/2
not defined $value and $self->error
not defined $value $self->error
Truth table66%2/3
not(A) and B
A= not defined $valueB= $self->error
ABresult
0X0
100
111
321 169 ▶ $count++ if( !defined( $value ) || !length( $value ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 86 83 169
Condition66%2/3
defined $value and length $value
!ll&&!rl&&r
86083
MC/DC50%1/2
defined $value and length $value
defined $value length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
322 }
323 40 return( $count );
324 }
325  
326 sub num_populated_fields
327 40 ▶{
Subroutine num_populated_fields: called CC=10 SCAR=24.5
Pod documented
328 40 my $self = shift( @_ );
329 40 my $name = shift( @_ );
330 40 my $args = $self->_get_args_as_hash( @_ );
331 40 my $resolver = delete( $args->{modifier_resolver} );
332 40 ▶ if( scalar( keys( %$args ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$args) 0 40 40
333 {
334 0 return( $self->error( "Unknown num_populated_fields option '", ( keys( %$args ) )[0], "'." ) );
335 }
336 40 ▶ unless( Scalar::Util::blessed( $name ) &&
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($name) and "PersonName::Format::Name"->implements_name_contract($name)) 0 40 40
Condition33%1/3
builtin::blessed($name) and "PersonName::Format::Name"->implements_name_contract($name)
!ll&&!rl&&r
0040
MC/DC0%0/2
builtin::blessed($name) and "PersonName::Format::Name"->implements_name_contract($name)
builtin::blessed($name) "PersonName::Format::Name"->implements_name_contract($name)
Truth table33%1/3
A and B
A= builtin::blessed($name)B= "PersonName::Format::Name"->implements_name_contract($name)
ABresult
0X0
100
111
337 PersonName::Format::Name->implements_name_contract( $name ) )
338 {
339 0 return( $self->error( "No valid name object was provided." ) );
340 }
341 40 $name = $self->_prepare_name( $name );
342 40 my $count = 0;
343 40 foreach my $token ( @{$self->{tokens}} )
344 {
345 288 ▶ next unless( $token->{type} eq 'field' );
Branch: 1 branch
BranchTrueFalseTotal
unless $token->{"type"} eq "field" 119 169 288
346 169 my $value = $self->_field_value( $name, $token, $resolver );
347 169 ▶ return( $self->pass_error ) if( !defined( $value ) && $self->error );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value and $self->error 0 169 169
Condition66%2/3
not defined $value and $self->error
!ll&&!rl&&r
83860
MC/DC0%0/2
not defined $value and $self->error
not defined $value $self->error
Truth table66%2/3
not(A) and B
A= not defined $valueB= $self->error
ABresult
0X0
100
111
348 169 ▶ $count++ if( defined( $value ) && length( $value ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and length $value 83 86 169
Condition66%2/3
defined $value and length $value
!ll&&!rl&&r
86083
MC/DC50%1/2
defined $value and length $value
defined $value length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
349 }
350 40 return( $count );
351 }
352  
353 sub pattern
354 4 ▶{
Subroutine pattern: called CC=1 SCAR=0.0
Pod documented
355 4 return( shift->_set_get_scalar( 'pattern', @_ ) );
356 }
357  
358 sub select_best
359 36 ▶{
Subroutine select_best: called CC=16 SCAR=29.1
Pod documented
360 36 my $self = shift( @_ );
361 36 my $patterns = shift( @_ );
362 36 my $name = shift( @_ );
363 36 my $args = $self->_get_args_as_hash( @_ );
364 36 ▶ unless( ref( $patterns ) eq 'ARRAY' && @$patterns )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $patterns eq "ARRAY" and @$patterns) 0 36 36
Condition33%1/3
ref $patterns eq "ARRAY" and @$patterns
!ll&&!rl&&r
0036
MC/DC0%0/2
ref $patterns eq "ARRAY" and @$patterns
ref $patterns eq "ARRAY" @$patterns
Truth table33%1/3
A and B
A= ref $patterns eq "ARRAY"B= @$patterns
ABresult
0X0
100
111
365 {
366 0 return( $self->error( "Patterns must be provided as an array reference." ) );
367 }
368 36 my $best;
369 36 my $best_populated = -1;
370 36 my $best_empty;
371  
372 36 foreach my $pattern ( @$patterns )
373 {
374 39 ▶ $pattern = __PACKAGE__->new( $pattern ) unless( Scalar::Util::blessed( $pattern ) );
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($pattern) 5 34 39
375 39 ▶ return( $self->pass_error( __PACKAGE__->error ) ) unless( defined( $pattern ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pattern 0 39 39
376 39 ▶ unless( $pattern->isa( __PACKAGE__ ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($pattern->isa(__PACKAGE__)) 0 39 39
377 {
378 0 return( $self->error( "Pattern list contains an unsupported object." ) );
379 }
380 39 my $populated = $pattern->num_populated_fields( $name, %$args );
381 39 ▶ return( $self->pass_error( $pattern->error ) ) unless( defined( $populated ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $populated 0 39 39
382 39 my $empty = $pattern->num_empty_fields( $name, %$args );
383 39 ▶ return( $self->pass_error( $pattern->error ) ) unless( defined( $empty ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $empty 0 39 39
384  
385 39 ▶ if( !defined( $best ) ||
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $best or $populated > $best_populated or $populated == $best_populated and $empty < $best_empty or $populated == $best_populated and $empty == $best_empty and $pattern->pattern lt $best->pattern) 38 1 39
Condition83%15/18
not defined $best or $populated > $best_populated or $populated == $best_populated and $empty < $best_empty or $populated == $best_populated and $empty == $best_empty and $pattern->pattern lt $best->pattern
l!l&&r!l&&!r
3711
not defined $best or $populated > $best_populated or $populated == $best_populated and $empty < $best_empty
l!l&&r!l&&!r
3612
not defined $best or $populated > $best_populated
l!l&&r!l&&!r
3603
$populated == $best_populated and $empty < $best_empty
!ll&&!rl&&r
111
$populated == $best_populated and $empty == $best_empty and $pattern->pattern lt $best->pattern
!ll&&!rl&&r
101
$populated == $best_populated and $empty == $best_empty
!ll&&!rl&&r
101
MC/DC42%3/7
not defined $best or $populated > $best_populated or $populated == $best_populated and $empty < $best_empty or $populated == $best_populated and $empty == $best_empty and $pattern->pattern lt $best->pattern
not defined $best $populated > $best_populated $populated == $best_populated $empty < $best_empty $populated == $best_populated $empty == $best_empty $pattern->pattern lt $best->pattern
Truth table36%4/11
not(A) or B or C and D or E and F and G
A= not defined $bestB= $populated > $best_populatedC= $populated == $best_populatedD= $empty < $best_emptyE= $populated == $best_populatedF= $empty == $best_emptyG= $pattern->pattern lt $best->pattern
ABCDEFGresult
1XXXXXX1
01XXXXX1
0011XXX1
000X1111
00101111
000X0XX0
000X10X0
000X1100
00100XX0
001010X0
00101100
386 $populated > $best_populated ||
387 ( $populated == $best_populated && $empty < $best_empty ) ||
388 ( $populated == $best_populated && $empty == $best_empty &&
389 $pattern->pattern lt $best->pattern ) )
390 {
391 38 $best = $pattern;
392 38 $best_populated = $populated;
393 38 $best_empty = $empty;
394 }
395 }
396 36 return( $best );
397 }
398  
399 sub tokens
400 1 ▶{
Subroutine tokens: called CC=3 SCAR=11.0
Pod documented
401 1 my $self = shift( @_ );
402 1 my $copy = [];
403 1 foreach my $token ( @{$self->{tokens}} )
404 {
405 9 my $item = {%$token};
406 9 ▶ if( exists( $token->{modifiers} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $token->{"modifiers"}) 5 4 9
407 {
408 5 $item->{modifiers} = [@{$token->{modifiers}}];
409 }
410 9 push( @$copy, $item );
411 }
412 1 return( $copy );
413 }
414  
415 sub _append_part
416 19 ▶{
Subroutine _append_part: called CC=8 SCAR=21.2
417 19 my $self = shift( @_ );
418 19 my $parts = shift( @_ );
419 19 my $type = shift( @_ );
420 19 my $value = shift( @_ );
421 19 my $token = shift( @_ );
422 19 ▶ return if( !defined( $value ) || !length( $value ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 7 12 19
Condition66%2/3
defined $value and length $value
!ll&&!rl&&r
0712
MC/DC50%1/2
defined $value and length $value
defined $value length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
423  
424 12 ▶ if( $type eq 'literal' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "literal" and @$parts and $parts->[-1]{"type"} eq "literal") 0 12 12
Condition66%4/6
$type eq "literal" and @$parts and $parts->[-1]{"type"} eq "literal"
!ll&&!rl&&r
840
$type eq "literal" and @$parts
!ll&&!rl&&r
804
MC/DC0%0/3
$type eq "literal" and @$parts and $parts->[-1]{"type"} eq "literal"
$type eq "literal" @$parts $parts->[-1]{"type"} eq "literal"
Truth table50%2/4
A and B and C
A= $type eq "literal"B= @$partsC= $parts->[-1]{"type"} eq "literal"
ABCresult
0XX0
10X0
1100
1111
425 @$parts &&
426 $parts->[-1]->{type} eq 'literal' )
427 {
428 0 $parts->[-1]->{value} .= $value;
429 0 return( $parts->[-1] );
430 }
431  
432 12 my $part =
433 {
434 type => $type,
435 value => $value,
436 };
437  
438 12 ▶ if( defined( $token ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token) 8 4 12
439 {
440 8 $part->{field} = $token->{field};
441 8 ▶ if( @{$token->{modifiers}} )
Branch: 1 branch
BranchTrueFalseTotal
if (@{$token->{"modifiers"};}) 2 6 8
442 {
443 2 $part->{modifiers} = [@{$token->{modifiers}}];
444 }
445 }
446  
447 12 push( @$parts, $part );
448 12 return( $part );
449 }
450  
451 sub _coalesce
452 19 ▶{
Subroutine _coalesce: called CC=7 SCAR=21.3
453 19 my $self = shift( @_ );
454 19 ▶ my $before = shift( @_ ) // '';
Condition50%1/2
shift @_ // ""
l!l
190
MC/DC0%0/1
shift @_ // ""
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
455 19 ▶ my $after = shift( @_ ) // '';
Condition50%1/2
shift @_ // ""
l!l
190
MC/DC0%0/1
shift @_ // ""
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
456  
457 # Remove punctuation immediately preceding the omitted field, stopping at the
458 # closest whitespace character or the preceding populated field.
459 19 $before =~ s/\S+$//;
460  
461 # Remove punctuation immediately following the omitted field, stopping at the
462 # closest whitespace character or the following populated field.
463 19 $after =~ s/^\S+//;
464  
465 # If the surviving text after the omitted field already occurs at the end of the
466 # surviving text before it, keep only the former text. This is the common case for
467 # two identical separating spaces.
468 19 ▶ if( length( $after ) <= length( $before ) &&
Branch: 2 branches
BranchTrueFalseTotal
if (length $after <= length $before and substr($before, length($before) - length($after)) eq $after) { } 18 1 19
elsif ($before =~ /\s\z/ and $after =~ /^\s/) { } 0 2 2
Condition66%2/3
length $after <= length $before and substr($before, length($before) - length($after)) eq $after
!ll&&!rl&&r
1018
MC/DC50%1/2
length $after <= length $before and substr($before, length($before) - length($after)) eq $after
length $after <= length $before substr($before, length($before) - length($after)) eq $after
Truth table66%2/3
A and B
A= length $after <= length $beforeB= substr($before, length($before) - length($after)) eq $after
ABresult
0X0
100
111
469 substr( $before, length( $before ) - length( $after ) ) eq $after )
470 {
471 18 $after = '';
472 }
473 ▶ elsif( $before =~ /\s\z/ && $after =~ /^\s/ )
Condition33%1/3
$before =~ /\s\z/ and $after =~ /^\s/
!ll&&!rl&&r
100
MC/DC0%0/2
$before =~ /\s\z/ and $after =~ /^\s/
$before =~ /\s\z/ $after =~ /^\s/
Truth table33%1/3
A and B
A= $before =~ /\s\z/B= $after =~ /^\s/
ABresult
0X0
100
111
474 {
475 # Otherwise coalesce the two adjacent whitespace characters to one.
476 0 substr( $after, 0, 1, '' );
477 }
478  
479 19 return( $before . $after );
480 }
481  
482 sub _field_value
483 480 ▶{
Subroutine _field_value: called CC=8 SCAR=20.8
484 480 my $self = shift( @_ );
485 480 my $name = shift( @_ );
486 480 my $token = shift( @_ );
487 480 my $resolver = shift( @_ );
488 480 my $modifiers = $self->_modifier_hash( $token->{modifiers} );
489 480 my $value = $name->get_field_value( $token->{field}, $modifiers );
490 480 ▶ return if( !defined( $value ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 237 243 480
491  
492 243 foreach my $modifier ( @{$token->{modifiers}} )
493 {
494 34 ▶ next unless( exists( $modifiers->{ $modifier } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $modifiers->{$modifier} 3 31 34
495 31 ▶ if( ref( $resolver ) eq 'CODE' )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $resolver eq 'CODE') { } 4 27 31
elsif (builtin::blessed($resolver) and $resolver->can('resolve')) { } 26 1 27
496 {
497 $value = $resolver->(
498 $modifier,
499 $value,
500 $token->{field},
501 4 $token,
502 $name,
503 );
504 }
505 ▶ elsif( Scalar::Util::blessed( $resolver ) &&
Condition66%2/3
builtin::blessed($resolver) and $resolver->can("resolve")
!ll&&!rl&&r
1026
MC/DC50%1/2
builtin::blessed($resolver) and $resolver->can("resolve")
builtin::blessed($resolver) $resolver->can("resolve")
Truth table66%2/3
A and B
A= builtin::blessed($resolver)B= $resolver->can("resolve")
ABresult
0X0
100
111
506 $resolver->can( 'resolve' ) )
507 {
508 $value = $resolver->resolve(
509 $modifier,
510 $value,
511 $token->{field},
512 26 $token,
513 $name,
514 );
515 }
516 else
517 {
518 1 return( $self->error( "No modifier resolver was provided for modifier '${modifier}' in field '", $token->{source}, "'." ) );
519 }
520 30 ▶ return if( !defined( $value ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 30 30
521 30 delete( $modifiers->{ $modifier } );
522 }
523 242 return( $value );
524 }
525  
526 sub _has_non_initial_given
527 2 ▶{
Subroutine _has_non_initial_given: called CC=5 SCAR=16.1
528 2 my $self = shift( @_ );
529 2 foreach my $token ( @{$self->{tokens}} )
530 {
531 4 ▶ unless( $token->{type} eq 'field' &&
Branch: 1 branch
BranchTrueFalseTotal
unless ($token->{"type"} eq "field" and $token->{"field"} eq "given") 2 2 4
Condition100%3/3
$token->{"type"} eq "field" and $token->{"field"} eq "given"
!ll&&!rl&&r
112
MC/DC100%2/2
$token->{"type"} eq "field" and $token->{"field"} eq "given"
$token->{"type"} eq "field" $token->{"field"} eq "given"
Truth table100%3/3
A and B
A= $token->{"type"} eq "field"B= $token->{"field"} eq "given"
ABresult
0X0
100
111
532 $token->{field} eq 'given' )
533 {
534 2 next;
535 }
536 2 my $mods = {};
537 2 @$mods{ @{$token->{modifiers}} } = (1) x scalar( @{$token->{modifiers}} );
538 2 ▶ unless( exists( $mods->{initial} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $mods->{"initial"}) 1 1 2
539 {
540 1 return(1);
541 }
542 }
543 1 return(0);
544 }
545  
546 sub _modifier_hash
547 480 ▶{
Subroutine _modifier_hash: called CC=1 SCAR=0.0
548 480 my $self = shift( @_ );
549 480 my $modifiers = shift( @_ );
550 480 my $hash = {};
551 480 @$hash{ @$modifiers } = (1) x scalar( @$modifiers );
552 480 return( $hash );
553 }
554  
555 sub _parse
556 57 ▶{
Subroutine _parse: called CC=15 SCAR=27.1
557 57 my $self = shift( @_ );
558 57 my $pattern = shift( @_ );
559 57 my $tokens = [];
560 57 my $buffer = '';
561 57 my $in_field = 0;
562 57 my $escaped = 0;
563  
564 57 foreach my $char ( split( //, $pattern ) )
565 {
566 1793 ▶ if( $escaped )
Branch: 1 branch
BranchTrueFalseTotal
if ($escaped) 2 1791 1793
567 {
568 2 $buffer .= $char;
569 2 $escaped = 0;
570 2 next;
571 }
572 1791 ▶ if( $char eq '\\' )
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "\\") 2 1789 1791
573 {
574 2 $escaped = 1;
575 2 next;
576 }
577 1789 ▶ if( $char eq '{' )
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "{") 158 1631 1789
578 {
579 158 ▶ if( $in_field )
Branch: 1 branch
BranchTrueFalseTotal
if ($in_field) 1 157 158
580 {
581 1 return( $self->error( "Nested braces are not allowed in person-name patterns." ) );
582 }
583 157 ▶ if( length( $buffer ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer) 94 63 157
584 {
585 94 push( @$tokens, { type => 'literal', value => $buffer } );
586 }
587 157 $buffer = '';
588 157 $in_field = 1;
589 157 next;
590 }
591 1631 ▶ if( $char eq '}' )
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "}") 156 1475 1631
592 {
593 156 ▶ unless( $in_field )
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_field) 1 155 156
594 {
595 1 return( $self->error( "Unmatched closing brace in person-name pattern." ) );
596 }
597 155 ▶ unless( length( $buffer ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (length $buffer) 1 154 155
598 {
599 1 return( $self->error( "No field name was provided inside braces." ) );
600 }
601 154 ▶ my $token = $self->_parse_field( $buffer ) ||
Condition100%2/2
$self->_parse_field($buffer) || (return $self->pass_error)
l!l
1459
MC/DC100%1/1
$self->_parse_field($buffer) || (return $self->pass_error)
$self->_parse_field($buffer)
Truth table100%2/2
A
A= $self->_parse_field($buffer)
Aresult
11
00
602 return( $self->pass_error );
603 145 push( @$tokens, $token );
604 145 $buffer = '';
605 145 $in_field = 0;
606 145 next;
607 }
608 1475 $buffer .= $char;
609 }
610  
611 45 ▶ if( $escaped )
Branch: 2 branches
BranchTrueFalseTotal
if ($escaped) { } 0 45 45
elsif ($in_field) { } 1 44 45
612 {
613 0 return( $self->error( "A trailing escape character is not allowed in a person-name pattern." ) );
614 }
615 elsif( $in_field )
616 {
617 1 return( $self->error( "Unmatched opening brace in person-name pattern." ) );
618 }
619  
620 44 ▶ if( length( $buffer ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer) 1 43 44
621 {
622 1 push( @$tokens, { type => 'literal', value => $buffer } );
623 }
624  
625 44 ▶ unless( scalar( grep{ $_->{type} eq 'field' } @$tokens ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar grep({$_->{"type"} eq "field";} @$tokens)) 1 43 44
626 {
627 1 return( $self->error( "A person-name pattern must contain at least one field." ) );
628 }
629 43 return( $tokens );
630 }
631  
632 sub _parse_field
633 154 ▶{
Subroutine _parse_field: called CC=11 SCAR=24.0
634 154 my $self = shift( @_ );
635 154 my $definition = shift( @_ );
636 154 my @parts = split( /-/, $definition, -1 );
637 154 my $field = shift( @parts );
638 154 ▶ unless( exists( $FIELDS->{ $field } ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $FIELDS->{$field}) 3 151 154
639 {
640 3 return( $self->error( "Unknown person-name field '${field}'." ) );
641 }
642 151 my $seen = {};
643 151 foreach my $modifier ( @parts )
644 {
645 40 ▶ if( !length( $modifier ) )
Branch: 3 branches
BranchTrueFalseTotal
if (not length $modifier) { } 0 40 40
elsif (not exists $MODIFIERS->{$modifier}) { } 1 39 40
elsif ($seen->{$modifier}++) { } 1 38 39
646 {
647 0 return( $self->error( "An empty modifier was found in field '${definition}'." ) );
648 }
649 elsif( !exists( $MODIFIERS->{ $modifier } ) )
650 {
651 1 return( $self->error( "Unknown person-name modifier '${modifier}' in field '${definition}'." ) );
652 }
653 elsif( $seen->{ $modifier }++ )
654 {
655 1 return( $self->error( "Modifier '${modifier}' occurs more than once in field '${definition}'." ) );
656 }
657 }
658 149 foreach my $pair (
659 [qw( allCaps initialCap )],
660 [qw( initial monogram )],
661 [qw( prefix core )],
662 )
663 {
664 444 ▶ if( exists( $seen->{ $pair->[0] } ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$seen{$pair->[0]} and exists $$seen{$pair->[1]}) 3 441 444
Condition100%3/3
exists $$seen{$pair->[0]} and exists $$seen{$pair->[1]}
!ll&&!rl&&r
419223
MC/DC100%2/2
exists $$seen{$pair->[0]} and exists $$seen{$pair->[1]}
exists $$seen{$pair->[0]} exists $$seen{$pair->[1]}
Truth table100%3/3
A and B
A= exists $$seen{$pair->[0]}B= exists $$seen{$pair->[1]}
ABresult
0X0
100
111
665 exists( $seen->{ $pair->[1] } ) )
666 {
667 3 return( $self->error( "Modifiers '$pair->[0]' and '$pair->[1]' are mutually exclusive in field '${definition}'." ) );
668 }
669 }
670 146 ▶ if( exists( $seen->{retain} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen->{"retain"} and not exists $seen->{"initial"}) 1 145 146
Condition100%3/3
exists $seen->{"retain"} and not exists $seen->{"initial"}
!ll&&!rl&&r
14321
MC/DC100%2/2
exists $seen->{"retain"} and not exists $seen->{"initial"}
exists $seen->{"retain"} not exists $seen->{"initial"}
Truth table100%3/3
A and not(B)
A= exists $seen->{"retain"}B= not exists $seen->{"initial"}
ABresult
0X0
100
111
671 !exists( $seen->{initial} ) )
672 {
673 1 return( $self->error( "Modifier 'retain' requires modifier 'initial' in field '${definition}'." ) );
674 }
675 return(
676 {
677 145 type => 'field',
678 field => $field,
679 modifiers => [@parts],
680 source => $definition,
681 });
682 }
683  
684 sub _prepare_name
685 119 ▶{
Subroutine _prepare_name: called CC=3 SCAR=11.0
686 119 my $self = shift( @_ );
687 119 my $name = shift( @_ );
688 119 my $surname = $name->get_field_value( 'surname', {} );
689 119 ▶ return( $name ) if( defined( $surname ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $surname 117 2 119
690 2 ▶ return( $name ) if( $self->_has_non_initial_given );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_non_initial_given 1 1 2
691 1 return( PersonName::Format::Pattern::GivenToSurname->new( $name ) );
692 }
693  
694 # NOTE: PersonName::Format::Pattern::GivenToSurname class
695 package PersonName::Format::Pattern::GivenToSurname;
696 BEGIN
697 {
698 19 ▶ use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
699 19 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
700 19 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
701 19 ▶ use parent qw( PersonName::Format::Name );
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: not called CC=1 SCAR=0.0
702 };
703  
704 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
705 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
706  
707 sub init
708 1 ▶{
Subroutine init: called CC=2 SCAR=7.0
709 1 my $self = shift( @_ );
710 1 my $name = shift( @_ );
711 1 ▶ return( $self->error( "No underlying name object was provided." ) )
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($name) 0 1 1
712 unless( Scalar::Util::blessed( $name ) );
713 1 $self->{name} = $name;
714 1 return( $self );
715 }
716  
717 sub get_field_value
718 2 ▶{
Subroutine get_field_value: called CC=3 SCAR=11.2
719 2 my $self = shift( @_ );
720 2 my $field = shift( @_ );
721 2 my $modifiers = shift( @_ );
722 2 ▶ return( $self->{name}->get_field_value( 'given', $modifiers ) )
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "surname" 1 1 2
723 if( $field eq 'surname' );
724 1 ▶ return if( $field eq 'given' );
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "given" 1 0 1
725 0 return( $self->{name}->get_field_value( $field, $modifiers ) );
726 }
727  
728 sub name_locale
729 0 ▶{
Subroutine name_locale: not called CC=1 SCAR=6.9
730 0 return( shift->{name}->name_locale( @_ ) );
731 }
732  
733 sub preferred_order
734 0 ▶{
Subroutine preferred_order: not called CC=1 SCAR=6.9
735 0 return( shift->{name}->preferred_order( @_ ) );
736 }
737  
738 19 1;
739 # NOTE: POD
740 __END__
« lib/PersonName/Format/FieldModifier.pm ↑ Summary lib/PersonName/Format/Exception.pm »