blib/lib/Gedcom.pm

filter: Statementstmt 93.6% 278 / 297 Branchbran 66.3% 77 / 116 Conditioncond 50.0% 31 / 62 MC/DCmcdc 27.7% 10 / 36 Subroutinesub 82.0% 41 / 50 Podpod 69.2% 18 / 26 totaltotal 77.5% 455 / 587 CC 91 SCAR 24.9
CC
91
Cov
81%
write_xml
42.0
new
29.4
get_individual
19.9
CRAP 12.1
« parse_grammar ↑ Summary blib/lib/Gedcom/Grammar.pm »
1 # Copyright 1998-2019, Paul Johnson (paul@pjcj.net)
2  
3 # This software is free. It is licensed under the same terms as Perl itself.
4  
5 # The latest version of this software should be available from my homepage:
6 # http://www.pjcj.net
7  
8 # documentation at __END__
9  
10 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 15 require 5.005;
13  
14 package Gedcom;
15  
16 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 15 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 15 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 15 ▶BEGIN { eval "use Text::Soundex" } # We'll use this if it is available
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 15 ▶use vars qw($VERSION $AUTOLOAD %Funcs);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 15 my $Tags;
25 my %Top_tag_order;
26  
27 BEGIN {
28 15 ▶ $VERSION = "1.21";
Subroutine BEGIN: called CC=3 SCAR=11.0
29  
30 15 $Tags = {
31 ABBR => "Abbreviation",
32 ADDR => "Address",
33 ADOP => "Adoption",
34 ADR1 => "Address1",
35 ADR2 => "Address2",
36 AFN => "Afn",
37 AGE => "Age",
38 AGNC => "Agency",
39 ALIA => "Alias",
40 ANCE => "Ancestors",
41 ANCI => "Ances Interest",
42 ANUL => "Annulment",
43 ASSO => "Associates",
44 AUTH => "Author",
45 BAPL => "Baptism-LDS",
46 BAPM => "Baptism",
47 BARM => "Bar Mitzvah",
48 BASM => "Bas Mitzvah",
49 BIRT => "Birth",
50 BLES => "Blessing",
51 BLOB => "Binary Object",
52 BURI => "Burial",
53 CALN => "Call Number",
54 CAST => "Caste",
55 CAUS => "Cause",
56 CENS => "Census",
57 CHAN => "Change",
58 CHAR => "Character",
59 CHIL => "Child",
60 CHR => "Christening",
61 CHRA => "Adult Christening",
62 CITY => "City",
63 CONC => "Concatenation",
64 CONF => "Confirmation",
65 CONL => "Confirmation L",
66 CONT => "Continued",
67 COPR => "Copyright",
68 CORP => "Corporate",
69 CREM => "Cremation",
70 CTRY => "Country",
71 DATA => "Data",
72 DATE => "Date",
73 DEAT => "Death",
74 DESC => "Descendants",
75 DESI => "Descendant Int",
76 DEST => "Destination",
77 DIV => "Divorce",
78 DIVF => "Divorce Filed",
79 DSCR => "Phy Description",
80 EDUC => "Education",
81 EMIG => "Emigration",
82 ENDL => "Endowment",
83 ENGA => "Engagement",
84 EVEN => "Event",
85 FAM => "Family",
86 FAMC => "Family Child",
87 FAMF => "Family File",
88 FAMS => "Family Spouse",
89 FCOM => "First Communion",
90 FILE => "File",
91 FORM => "Format",
92 GEDC => "Gedcom",
93 GIVN => "Given Name",
94 GRAD => "Graduation",
95 HEAD => "Header",
96 HUSB => "Husband",
97 IDNO => "Ident Number",
98 IMMI => "Immigration",
99 INDI => "Individual",
100 LANG => "Language",
101 LEGA => "Legatee",
102 MARB => "Marriage Bann",
103 MARC => "Marr Contract",
104 MARL => "Marr License",
105 MARR => "Marriage",
106 MARS => "Marr Settlement",
107 MEDI => "Media",
108 NAME => "Name",
109 NATI => "Nationality",
110 NATU => "Naturalization",
111 NCHI => "Children_count",
112 NICK => "Nickname",
113 NMR => "Marriage_count",
114 NOTE => "Note",
115 NPFX => "Name_prefix",
116 NSFX => "Name_suffix",
117 OBJE => "Object",
118 OCCU => "Occupation",
119 ORDI => "Ordinance",
120 ORDN => "Ordination",
121 PAGE => "Page",
122 PEDI => "Pedigree",
123 PHON => "Phone",
124 PLAC => "Place",
125 POST => "Postal_code",
126 PROB => "Probate",
127 PROP => "Property",
128 PUBL => "Publication",
129 QUAY => "Quality Of Data",
130 REFN => "Reference",
131 RELA => "Relationship",
132 RELI => "Religion",
133 REPO => "Repository",
134 RESI => "Residence",
135 RESN => "Restriction",
136 RETI => "Retirement",
137 RFN => "Rec File Number",
138 RIN => "Rec Id Number",
139 ROLE => "Role",
140 SEX => "Sex",
141 SLGC => "Sealing Child",
142 SLGS => "Sealing Spouse",
143 SOUR => "Source",
144 SPFX => "Surn Prefix",
145 SSN => "Soc Sec Number",
146 STAE => "State",
147 STAT => "Status",
148 SUBM => "Submitter",
149 SUBN => "Submission",
150 SURN => "Surname",
151 TEMP => "Temple",
152 TEXT => "Text",
153 TIME => "Time",
154 TITL => "Title",
155 TRLR => "Trailer",
156 TYPE => "Type",
157 VERS => "Version",
158 WIFE => "Wife",
159 WILL => "Will",
160 };
161  
162 15 %Top_tag_order = (
163 HEAD => 1,
164 SUBM => 2,
165 INDI => 3,
166 FAM => 4,
167 NOTE => 5,
168 REPO => 6,
169 SOUR => 7,
170 TRLR => 8,
171 );
172  
173 15 ▶ while (my ($tag, $name) = each (%$Tags)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$Tags) 1935 15 1950
174 1935 $Funcs{$tag} = $Funcs{lc $tag} = $tag;
175 1935 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 1935 0 1935
176 1935 $name =~ s/ /_/g;
177 1935 $Funcs{lc $name} = $tag;
178 }
179 }
180 }
181  
182 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
183  
184 sub AUTOLOAD {
185 10 ▶ my ($self) = @_; # don't change @_ because of the goto
Subroutine AUTOLOAD: called CC=5 SCAR=16.2
186 10 my $func = $AUTOLOAD;
187 # print "autoloading $func\n";
188 10 $func =~ s/^.*:://;
189 10 my $tag;
190 croak "Undefined subroutine $func called"
191 if $func !~ /^(add|get)_(.*)$/ ||
192 !($tag = $Funcs{lc $2}) ||
193 10 ▶ !exists $Top_tag_order{$tag};
Branch: 1 branch
BranchTrueFalseTotal
unless $func =~ /^(add|get)_(.*)$/ and $tag = $Gedcom::Funcs{lc $2} and exists $Top_tag_order{$tag} 0 10 10
Condition33%2/6
$func =~ /^(add|get)_(.*)$/ and $tag = $Gedcom::Funcs{lc $2} and exists $Top_tag_order{$tag}
!ll&&!rl&&r
0010
$func =~ /^(add|get)_(.*)$/ and $tag = $Gedcom::Funcs{lc $2}
!ll&&!rl&&r
0010
MC/DC0%0/3
$func =~ /^(add|get)_(.*)$/ and $tag = $Gedcom::Funcs{lc $2} and exists $Top_tag_order{$tag}
$func =~ /^(add|get)_(.*)$/ $tag = $Gedcom::Funcs{lc $2} exists $Top_tag_order{$tag}
Truth table25%1/4
A and B and C
A= $func =~ /^(add|get)_(.*)$/B= $tag = $Gedcom::Funcs{lc $2}C= exists $Top_tag_order{$tag}
ABCresult
0XX0
10X0
1100
1111
194 15 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
195 10 ▶ if ($1 eq "add") {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq 'add') { } 6 4 10
196 *$func = sub {
197 12 ▶ my $self = shift;
Subroutine __ANON__: called CC=9 SCAR=22.0
198 12 my ($arg, $val) = @_;
199 12 my $xref;
200 12 ▶ if (ref $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) { } 1 11 12
201 1 $xref = $arg->{xref};
202 } else {
203 11 $val = $arg;
204 }
205 12 my $record = $self->add_record(tag => $tag, val => $val);
206 12 ▶ if (defined $val && $tag eq "NOTE") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and $tag eq "NOTE") 2 10 12
Condition100%3/3
defined $val and $tag eq "NOTE"
!ll&&!rl&&r
822
MC/DC100%2/2
defined $val and $tag eq "NOTE"
defined $val $tag eq "NOTE"
Truth table100%3/3
A and B
A= defined $valB= $tag eq "NOTE"
ABresult
0X0
100
111
207 2 $record->{value} = $val;
208 }
209 12 ▶ $xref = $tag eq "SUBM" ? "SUBM" : substr $tag, 0, 1
Branch: 2 branches
BranchTrueFalseTotal
unless defined $xref 11 1 12
$tag eq 'SUBM' ? : 1 10 11
210 unless defined $xref;
211 12 ▶ unless ($tag =~ /^(HEAD|TRLR)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tag =~ /^(HEAD|TRLR)$/) 10 2 12
212 10 ▶ croak "Invalid xref $xref requested in $func"
Branch: 1 branch
BranchTrueFalseTotal
unless $xref =~ /^[^\W\d_]+(\d*)$/ 0 10 10
213 unless $xref =~ /^[^\W\d_]+(\d*)$/;
214 10 ▶ $xref = $self->next_xref($xref) unless length $1;
Branch: 1 branch
BranchTrueFalseTotal
unless length $1 9 1 10
215 10 $record->{xref} = $xref;
216 10 $self->{xrefs}{$xref} = $record;
217 }
218 $record
219 6 };
220 } else {
221 *$func = sub {
222 4 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=14.4
223 4 my ($xref) = @_;
224 4 my $nxr = !defined $xref;
225 584 ▶ my @a = grep { $_->{tag} eq $tag && ($nxr || $_->{xref} eq $xref) }
Branch: 1 branch
BranchTrueFalseTotal
$_->{"tag"} eq $tag and $nxr || $_->{"xref"} eq $xref 4 580 584
Condition50%3/6
$_->{'tag'} eq $tag && ($nxr || $_->{'xref'} eq $xref)
!ll&&!rl&&r
58004
$nxr or $_->{"xref"} eq $xref
l!l&&r!l&&!r
040
MC/DC33%1/3
$_->{'tag'} eq $tag && ($nxr || $_->{'xref'} eq $xref)
$_->{'tag'} eq $tag $nxr $_->{"xref"} eq $xref
Truth table50%2/4
A && B or C
A= $_->{'tag'} eq $tagB= $nxrC= $_->{"xref"} eq $xref
ABCresult
0XX0
1000
11X1
1011
226 4 @{$self->{record}->_items};
227 4 ▶ wantarray ? @a : $a[0]
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 4 4
228 4 };
229 }
230 10 goto &$func
231 }
232  
233 15 ▶use Gedcom::Grammar 1.21;
Subroutine BEGIN: called CC=1 SCAR=0.0
234 15 ▶use Gedcom::Individual 1.21;
Subroutine BEGIN: called CC=1 SCAR=0.0
235 15 ▶use Gedcom::Family 1.21;
Subroutine BEGIN: called CC=1 SCAR=0.0
236 15 ▶use Gedcom::Event 1.21;
Subroutine BEGIN: called CC=1 SCAR=0.0
237  
238 sub new {
239 11 ▶ my $proto = shift;
Subroutine new: called CC=25 SCAR=33.8
Pod documented
240 11 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0110
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
241 11 ▶ @_ = (gedcom_file => @_) if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 11 11
242 11 my $self = {
243 records => [],
244 tags => $Tags,
245 read_only => 0,
246 types => {},
247 xrefs => {},
248 encoding => "ansel",
249 @_
250 };
251  
252 # TODO - find a way to do this nicely for different grammars
253 11 $self->{types}{INDI} = "Individual";
254 11 $self->{types}{FAM} = "Family";
255 11 $self->{types}{$_} = "Event" for qw(
256 ADOP ANUL BAPM BARM BASM BIRT BLES BURI CAST CENS CENS CHR CHRA CONF
257 CREM DEAT DIV DIVF DSCR EDUC EMIG ENGA EVEN EVEN FCOM GRAD IDNO IMMI
258 MARB MARC MARL MARR MARS NATI NATU NCHI NMR OCCU ORDN PROB PROP RELI
259 RESI RETI SSN WILL
260 );
261 11 bless $self, $class;
262  
263 # first read in the grammar
264 11 my $grammar;
265 11 ▶ if (defined $self->{grammar_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'grammar_file'}) { } 1 10 11
266 1 my $version;
267 1 ▶ if (defined $self->{grammar_version}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'grammar_version'}) { } 0 1 1
268 0 $version = $self->{grammar_version};
269 } else {
270 1 ($version) = $self->{grammar_file} =~ /(\d+(\.\d+)*)/;
271 }
272 1 ▶ die "version must be a GEDCOM version number\n" unless $version;
Branch: 1 branch
BranchTrueFalseTotal
unless $version 0 1 1
273 return undef unless
274 $grammar = Gedcom::Grammar->new(
275 file => $self->{grammar_file},
276 version => $version,
277 callback => $self->{callback}
278 1 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
unless $grammar = "Gedcom::Grammar"->new("file", $self->{"grammar_file"}, "version", $version, "callback", $self->{"callback"}) 0 1 1
279 } else {
280 10 ▶ $self->{grammar_version} = 5.5 unless defined $self->{grammar_version};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"grammar_version"} 9 1 10
281 10 (my $v = $self->{grammar_version}) =~ tr/./_/;
282 10 my $g = "Gedcom::Grammar_$v";
283 10 ▶ eval "use $g $VERSION";
Subroutine BEGIN: called CC=1 SCAR=0.0
284 10 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 10 10
285 15 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
286 10 ▶ return undef unless $grammar = ${$g . "::grammar"};
Branch: 1 branch
BranchTrueFalseTotal
unless $grammar = ${$g . '::grammar';} 0 10 10
287 }
288 11 my @c = ($self->{grammar} = $grammar);
289 11 ▶ while (@c) {
Branch: 1 branch
BranchTrueFalseTotal
if (@c) 66 11 77
290 66 @c = map { $_->{top} = $grammar; @{$_->{items}} } @c;
291 }
292  
293 # now read in or create the GEDCOM file
294 return undef unless
295 my $r = $self->{record} = Gedcom::Record->new(
296 defined $self->{gedcom_file} ? (file => $self->{gedcom_file}) : (),
297 line => 0,
298 tag => "GEDCOM",
299 grammar => $grammar->structure("GEDCOM"),
300 gedcom => $self,
301 callback => $self->{callback},
302 11 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
unless my $r = $self->{"record"} = "Gedcom::Record"->new(defined $self->{"gedcom_file"} ? ("file", $self->{"gedcom_file"}) : (), "line", 0, "tag", "GEDCOM", "grammar", $grammar->structure("GEDCOM"), "gedcom", $self, "callback", $self->{"callback"}) 0 11 11
defined $self->{'gedcom_file'} ? : 10 1 11
303  
304 11 ▶ unless (defined $self->{gedcom_file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"gedcom_file"}) 1 10 11
305  
306 # Add the required elements, unless they are already there.
307  
308 1 ▶ unless ($r->get_record("head")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->get_record("head")) 1 0 1
309 1 my $me = "Unknown user";
310 1 my $login = $me;
311 1 ▶ if ($login = getlogin || (getpwuid($<))[0] ||
Branch: 1 branch
BranchTrueFalseTotal
if ($login = getlogin || (getpwuid $<)[0] || $ENV{"USER"} || $ENV{"LOGIN"}) 1 0 1
Condition33%3/9
getlogin || (getpwuid $<)[0] || $ENV{'USER'} || $ENV{'LOGIN'}
l!l&&r!l&&!r
100
getlogin or (getpwuid $<)[0] or $ENV{"USER"}
l!l&&r!l&&!r
100
getlogin or (getpwuid $<)[0]
l!l&&r!l&&!r
010
MC/DC0%0/4
getlogin || (getpwuid $<)[0] || $ENV{'USER'} || $ENV{'LOGIN'}
getlogin (getpwuid $<)[0] $ENV{"USER"} $ENV{'LOGIN'}
Truth table20%1/5
A or B or C || D
A= getloginB= (getpwuid $<)[0]C= $ENV{"USER"}D= $ENV{'LOGIN'}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
312 $ENV{USER} || $ENV{LOGIN}) {
313 1 my $name;
314 1 eval { $name = (getpwnam($login))[6] };
315 1 ▶ $me = $name || $login;
Condition33%1/3
$name || $login
l!l&&r!l&&!r
010
MC/DC0%0/2
$name || $login
$name $login
Truth table33%1/3
A || B
A= $nameB= $login
ABresult
1X1
011
000
316 }
317 1 my $date = localtime;
318  
319 1 my ($l0, $l1, $l2, $l3);
320 1 $l0 = $self->add_header;
321 1 $l1 = $l0->add("SOUR", "Gedcom.pm");
322 1 $l1->add("NAME", "Gedcom.pm");
323 1 $l1->add("VERS", $VERSION);
324 1 $l2 = $l1->add("CORP", "Paul Johnson");
325 1 $l2->add("ADDR", "http://www.pjcj.net");
326 1 $l2 = $l1->add("DATA");
327 1 $l3 = $l2->add(
328 "COPR",
329 'Copyright 1998-2019, Paul Johnson (paul@pjcj.net)'
330 );
331 1 $l1 = $l0->add("NOTE", "");
332 1 for (split /\n/, <<'EOH')
333 This output was generated by Gedcom.pm.
334 Gedcom.pm is Copyright 1998-2019, Paul Johnson (paul@pjcj.net)
335 Version 1.21 - 14th November 2019
336  
337 Gedcom.pm is free. It is licensed under the same terms as Perl itself.
338  
339 The latest version of Gedcom.pm should be available from my homepage:
340 http://www.pjcj.net
341 EOH
342 {
343 8 $l1->add("CONT", $_);
344 };
345 1 $l1 = $l0->add("GEDC");
346 1 $l1->add("VERS", $self->{grammar}{version});
347 1 $l1->add("FORM", "LINEAGE-LINKED");
348 1 $l0->add("DATE", $date);
349 1 ▶ $l0->add("CHAR", uc ($self->{encoding} || "ansel"));
Condition50%1/2
$self->{'encoding'} || 'ansel'
l!l
10
MC/DC0%0/1
$self->{'encoding'} || 'ansel'
$self->{'encoding'}
Truth table50%1/2
A
A= $self->{'encoding'}
Aresult
11
00
350 1 my $s = $r->get_record("subm");
351 1 ▶ unless ($s) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s) 1 0 1
352 1 $s = $self->add_submitter;
353 1 $s->add("NAME", $me);
354 }
355 1 $l0->add("SUBM", $s->xref);
356 }
357  
358 1 ▶ $self->add_trailer unless $r->get_record("trlr");
Branch: 1 branch
BranchTrueFalseTotal
unless $r->get_record("trlr") 1 0 1
359 }
360  
361 11 $self->collect_xrefs;
362  
363 11 $self
364 }
365  
366 sub set_encoding {
367 10 ▶ my $self = shift;
Subroutine set_encoding: called CC=1 SCAR=0.0
Pod documented
368 10 ($self->{encoding}) = @_;
369 }
370  
371 sub write {
372 7 ▶ my $self = shift;
Subroutine write: called CC=6 SCAR=19.9
Pod documented
373 7 ▶ my $file = shift or die "No filename specified";
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift() or die "No filename specified" 0 7 7
374 7 my $flush = shift;
375 7 ▶ $self->{fh} = FileHandle->new($file, "w") or die "Can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh"} = "FileHandle"->new($file, "w") or die "Can't open ${file}: $!" 0 7 7
376 binmode $self->{fh}, ":encoding(UTF-8)"
377 7 ▶ if $self->{encoding} eq "utf-8" && $] >= 5.8;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"encoding"} eq "utf-8" and $] >= "5.8" 0 7 7
Condition33%1/3
$self->{"encoding"} eq "utf-8" and $] >= "5.8"
!ll&&!rl&&r
700
MC/DC0%0/2
$self->{"encoding"} eq "utf-8" and $] >= "5.8"
$self->{"encoding"} eq "utf-8" $] >= "5.8"
Truth table33%1/3
A and B
A= $self->{"encoding"} eq "utf-8"B= $] >= "5.8"
ABresult
0X0
100
111
378 7 $self->{record}->write($self->{fh}, -1, $flush);
379 7 ▶ $self->{fh}->close or die "Can't close $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh"}->close or die "Can't close ${file}: $!" 0 7 7
380 }
381  
382 sub write_xml {
383 0 ▶ my $self = shift;
Subroutine write_xml: not called CC=6 SCAR=37.4
Pod documented
384 0 ▶ my $file = shift or die "No filename specified";
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift() or die "No filename specified" 0 0 0
385 0 ▶ $self->{fh} = FileHandle->new($file, "w") or die "Can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh"} = "FileHandle"->new($file, "w") or die "Can't open ${file}: $!" 0 0 0
386 binmode $self->{fh}, ":encoding(UTF-8)"
387 0 ▶ if $self->{encoding} eq "utf-8" && $] >= 5.8;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"encoding"} eq "utf-8" and $] >= "5.8" 0 0 0
Condition0%0/3
$self->{"encoding"} eq "utf-8" and $] >= "5.8"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"encoding"} eq "utf-8" and $] >= "5.8"
$self->{"encoding"} eq "utf-8" $] >= "5.8"
Truth table0%0/3
A and B
A= $self->{"encoding"} eq "utf-8"B= $] >= "5.8"
ABresult
0X0
100
111
388 0 $self->{fh}->print(<<'EOH');
389 <?xml version="1.0" encoding="utf-8"?>
390  
391 <!--
392  
393 This output was generated by Gedcom.pm.
394 Gedcom.pm is Copyright 1998-2019, Paul Johnson (paul@pjcj.net)
395 Version 1.21 - 14th November 2019
396  
397 Gedcom.pm is free. It is licensed under the same terms as Perl itself.
398  
399 The latest version of Gedcom.pm should be available from my homepage:
400 http://www.pjcj.net
401  
402 EOH
403 0 $self->{fh}->print("Generated on " . localtime() . "\n\n-->\n\n");
404 0 $self->{record}->write_xml($self->{fh});
405 0 ▶ $self->{fh}->close or die "Can't close $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh"}->close or die "Can't close ${file}: $!" 0 0 0
406 }
407  
408 sub add_record {
409 12 ▶ my $self = shift;
Subroutine add_record: called CC=1 SCAR=0.0
Pod documented
410 12 $self->{record}->add_record(@_);
411 }
412  
413 sub collect_xrefs {
414 24 ▶ my $self = shift;
Subroutine collect_xrefs: called CC=1 SCAR=0.0
Pod documented
415 24 my ($callback) = @_;
416 24 $self->{xrefs} = {};
417 24 $self->{record}->collect_xrefs($callback);
418 }
419  
420 sub resolve_xref {
421 29698 ▶ my $self = shift;;
Subroutine resolve_xref: called CC=3 SCAR=11.0
Pod documented
422 29698 my ($x) = @_;
423 29698 my $xref;
424 29698 ▶ $xref = $self->{xrefs}{$x =~ /^\@(.+)\@$/ ? $1 : $x} if defined $x;
Branch: 2 branches
BranchTrueFalseTotal
if defined $x 28943 755 29698
$x =~ /^\@(.+)\@$/ ? : 0 28943 28943
425 29698 $xref
426 }
427  
428 sub resolve_xrefs {
429 19 ▶ my $self = shift;
Subroutine resolve_xrefs: called CC=1 SCAR=0.0
Pod documented
430 19 my ($callback) = @_;
431 19 $self->{record}->resolve_xrefs($callback);
432 }
433  
434 sub unresolve_xrefs {
435 12 ▶ my $self = shift;
Subroutine unresolve_xrefs: called CC=1 SCAR=0.0
Pod documented
436 12 my ($callback) = @_;
437 12 $self->{record}->unresolve_xrefs($callback);
438 }
439  
440 sub validate {
441 61 ▶ my $self = shift;
Subroutine validate: called CC=3 SCAR=11.0
Pod documented
442 61 my ($callback) = @_;
443 61 $self->{validate_callback} = $callback;
444 61 my $ok = $self->{record}->validate_syntax;
445 61 for my $item (@{$self->{record}->_items}) {
446 8770 ▶ $ok = 0 unless $item->validate_semantics;
Branch: 1 branch
BranchTrueFalseTotal
unless $item->validate_semantics 0 8770 8770
447 }
448 $ok
449 61 }
450  
451 sub normalise_dates {
452 6 ▶ my $self = shift;
Subroutine normalise_dates: called CC=1 SCAR=0.0
Pod documented
453 6 $self->{record}->normalise_dates(@_);
454 }
455  
456 sub renumber {
457 13 ▶ my $self = shift;
Subroutine renumber: called CC=5 SCAR=16.1
Pod documented
458 13 my (%args) = @_;
459 13 $self->resolve_xrefs;
460  
461 # initially, renumber any records passed in
462 13 for my $xref (@{$args{xrefs}}) {
463 $self->{xrefs}{$xref}->renumber(\%args, 1)
464 6 ▶ if exists $self->{xrefs}{$xref};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"xrefs"}{$xref} 6 0 6
465 }
466  
467 # now, renumber any records left over
468 13 $_->renumber(\%args, 1) for @{$self->{record}->_items};
469  
470 # actually change the xref
471 13 for my $record (@{$self->{record}->_items}) {
472 1762 $record->{xref} = delete $record->{new_xref};
473 delete $record->{recursed}
474 1762 }
475  
476 # and update the xrefs
477 13 $self->collect_xrefs;
478  
479 13 %args
480 }
481  
482 sub sort_sub {
483 # subroutine to sort on tag order first, and then on xref
484  
485 my $t = sub {
486 6840 ▶ my ($r) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.7
487 6840 ▶ return -2 unless defined $r->{tag};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r->{"tag"} 0 6840 6840
488 6840 ▶ exists $Top_tag_order{$r->{tag}} ? $Top_tag_order{$r->{tag}} : -1
Branch: 1 branch
BranchTrueFalseTotal
exists $Top_tag_order{$r->{'tag'}} ? : 6840 0 6840
489 7 ▶ };
Subroutine sort_sub: called CC=1 SCAR=0.0
Pod documented
490  
491 my $x = sub {
492 6206 ▶ my ($r) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.4
493 6206 ▶ return -2 unless defined $r->{xref};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r->{"xref"} 0 6206 6206
494 6206 $r->{xref} =~ /(\d+)/;
495 6206 ▶ defined $1 ? $1 : -1
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 6206 0 6206
496 7 };
497  
498 sub {
499 3420 ▶ $t->($a) <=> $t->($b)
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
&$t($a) <=> &$t($b) or &$x($a) <=> &$x($b) 3103 317 3420
500 ||
501 $x->($a) <=> $x->($b)
502 }
503 7 }
504  
505 sub order {
506 7 ▶ my $self = shift;
Subroutine order: called CC=2 SCAR=7.2
Pod documented
507 7 ▶ my $sort_sub = shift || sort_sub; # use default sort unless one passed in
Condition33%1/3
shift() || sort_sub()
l!l&&r!l&&!r
070
MC/DC0%0/2
shift() || sort_sub()
shift() sort_sub()
Truth table33%1/3
A || B
A= shift()B= sort_sub()
ABresult
1X1
011
000
508 7 @{$self->{record}{items}} = sort $sort_sub @{$self->{record}->_items}
509 }
510  
511 sub items {
512 132 ▶ my $self = shift;
Subroutine items: called CC=1 SCAR=0.0
Pod undocumented
513 132 @{$self->{record}->_items}
514 }
515  
516 0 ▶sub heads { grep $_->tag eq "HEAD", shift->items }
Subroutine heads: not called CC=1 SCAR=6.9
Pod undocumented
517 0 ▶sub submitters { grep $_->tag eq "SUBM", shift->items }
Subroutine submitters: not called CC=1 SCAR=6.9
Pod undocumented
518 95 ▶sub individuals { grep ref eq "Gedcom::Individual", shift->items }
Subroutine individuals: called CC=1 SCAR=0.0
Pod documented
519 37 ▶sub families { grep ref eq "Gedcom::Family", shift->items }
Subroutine families: called CC=1 SCAR=0.0
Pod documented
520 0 ▶sub notes { grep $_->tag eq "NOTE", shift->items }
Subroutine notes: not called CC=1 SCAR=6.9
Pod undocumented
521 0 ▶sub repositories { grep $_->tag eq "REPO", shift->items }
Subroutine repositories: not called CC=1 SCAR=6.9
Pod undocumented
522 0 ▶sub sources { grep $_->tag eq "SOUR", shift->items }
Subroutine sources: not called CC=1 SCAR=6.9
Pod undocumented
523 0 ▶sub trailers { grep $_->tag eq "TRLR", shift->items }
Subroutine trailers: not called CC=1 SCAR=6.9
Pod undocumented
524  
525 sub get_individual {
526 86 ▶ my $self = shift;
Subroutine get_individual: called CC=19 SCAR=29.9
Pod documented
527 86 my $name = "@_";
528 86 my $all = wantarray;
529 86 my @i;
530  
531 86 ▶ my $i = $self->resolve_xref($name) || $self->resolve_xref(uc $name);
Condition66%2/3
$self->resolve_xref($name) || $self->resolve_xref(uc $name)
l!l&&r!l&&!r
30056
MC/DC50%1/2
$self->resolve_xref($name) || $self->resolve_xref(uc $name)
$self->resolve_xref($name) $self->resolve_xref(uc $name)
Truth table66%2/3
A || B
A= $self->resolve_xref($name)B= $self->resolve_xref(uc $name)
ABresult
1X1
011
000
532 86 ▶ if ($i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) 30 56 86
533 30 ▶ return $i unless $all;
Branch: 1 branch
BranchTrueFalseTotal
unless $all 30 0 30
534 0 push @i, $i;
535 }
536  
537 # search for the name in the specified order
538 my $ordered = sub {
539 423 ▶ my ($n, @ind) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
540 423 ▶ map { $_->[1] } grep { $_ && $_->[0] =~ $n } @ind
Branch: 1 branch
BranchTrueFalseTotal
$_ and $_->[0] =~ /$n/ 37611 0 37611
541 56 };
542  
543 # search for the name in any order
544 my $unordered = sub {
545 198 ▶ my ($names, $t, @ind) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
546 222 map { $_->[1] } grep {
547 198 my $i = $_->[0];
548 18414 my $r = 1;
549 18414 for my $n (@$names) {
550 # remove matches as they are found
551 # we don't want to match the same name twice
552 18858 ▶ last unless $r = $i =~ s/$n->[$t]//;
Branch: 1 branch
BranchTrueFalseTotal
unless $r = $i =~ s/$n->[$t]// 18192 666 18858
553 }
554 $r
555 18414 }
556 @ind;
557 56 };
558  
559 # look for various matches in decreasing order of exactitude
560 56 my @individuals = $self->individuals;
561  
562 # Store the name with the individual to avoid continually recalculating it.
563 # This is a bit like a Schwartzian transform, with a grep instead of a sort.
564 my @ind =
565 56 ▶ map [do { my $n = $_->tag_value("NAME"); defined $n ? $n : "" } => $_],
Branch: 1 branch
BranchTrueFalseTotal
defined $n ? : 5208 0 5208
566 @individuals;
567  
568 56 for my $n (map { qr/^$_$/, qr/\b$_\b/, $_ } map { $_, qr/$_/i } qr/\Q$name/)
569 {
570 327 push @i, $ordered->($n, @ind);
571 327 ▶ return $i[0] if !$all && @i;
Branch: 1 branch
BranchTrueFalseTotal
if not $all and @i 2 325 327
Condition100%3/3
not $all and @i
!ll&&!rl&&r
288372
MC/DC100%2/2
not $all and @i
not $all @i
Truth table100%3/3
not(A) and B
A= not $allB= @i
ABresult
0X0
100
111
572 }
573  
574 # create an array with one element per name
575 # each element is an array of REs in decreasing order of exactitude
576 54 my @names = map [ map { qr/\b$_\b/, $_ } map { qr/$_/, qr/$_/i } "\Q$_" ],
577 split / /, $name;
578 54 for my $t (0 .. $#{$names[0]}) {
579 198 push @i, $unordered->(\@names, $t, @ind);
580 198 ▶ return $i[0] if !$all && @i;
Branch: 1 branch
BranchTrueFalseTotal
if not $all and @i 6 192 198
Condition66%2/3
not $all and @i
!ll&&!rl&&r
19206
MC/DC50%1/2
not $all and @i
not $all @i
Truth table66%2/3
not(A) and B
A= not $allB= @i
ABresult
0X0
100
111
581 }
582  
583 # check soundex
584 48 ▶ my @sdx = map { my $s = $_->soundex; $s ? [ $s => $_ ] : () } @individuals;
Branch: 1 branch
BranchTrueFalseTotal
$s ? : 3600 864 4464
585  
586 48 my $soundex = soundex($name);
587 48 ▶ for my $n ( map { qr/$_/ } $name, ($soundex || ()) ) {
Condition33%1/3
$soundex || ()
l!l&&r!l&&!r
4800
MC/DC0%0/2
$soundex || ()
$soundex ()
Truth table33%1/3
A || B
A= $soundexB= ()
ABresult
1X1
011
000
588 96 push @i, $ordered->($n, @sdx);
589 96 ▶ return $i[0] if !$all && @i;
Branch: 1 branch
BranchTrueFalseTotal
if not $all and @i 0 96 96
Condition33%1/3
not $all and @i
!ll&&!rl&&r
9600
MC/DC0%0/2
not $all and @i
not $all @i
Truth table33%1/3
not(A) and B
A= not $allB= @i
ABresult
0X0
100
111
590 }
591  
592 48 ▶ return undef unless $all;
Branch: 1 branch
BranchTrueFalseTotal
unless $all 0 48 48
593  
594 48 my @s;
595 my %s;
596 48 for (@i) {
597 360 ▶ unless (exists $s{$_->{xref}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $s{$_->{"xref"}}) 114 246 360
598 114 push @s, $_;
599 114 $s{$_->{xref}}++;
600 }
601 }
602  
603 @s
604 48 }
605  
606 sub next_xref {
607 45 ▶ my $self = shift;
Subroutine next_xref: called CC=5 SCAR=16.1
Pod documented
608 45 my ($type) = @_;
609 45 my $re = qr/^$type(\d+)$/;
610 45 my $last = 0;
611 45 for my $c (@{$self->{record}->_items}) {
612 5318 ▶ $last = $1 if defined $c->{xref} and $c->{xref} =~ /$re/ and $1 > $last;
Branch: 1 branch
BranchTrueFalseTotal
if defined $c->{"xref"} and $c->{"xref"} =~ /$re/ and $1 > $last 1274 4044 5318
Condition100%6/6
defined $c->{"xref"} and $c->{"xref"} =~ /$re/ and $1 > $last
!ll&&!rl&&r
36124321274
defined $c->{"xref"} and $c->{"xref"} =~ /$re/
!ll&&!rl&&r
9835141706
MC/DC100%3/3
defined $c->{"xref"} and $c->{"xref"} =~ /$re/ and $1 > $last
defined $c->{"xref"} $c->{"xref"} =~ /$re/ $1 > $last
Truth table100%4/4
A and B and C
A= defined $c->{"xref"}B= $c->{"xref"} =~ /$re/C= $1 > $last
ABCresult
0XX0
10X0
1100
1111
613 }
614 45 $type . ++$last
615 }
616  
617 sub top_tag {
618 0 ▶ my $self = shift;
Subroutine top_tag: not called CC=1 SCAR=6.9
Pod undocumented
619 0 my ($tag) = @_;
620 0 $Top_tag_order{$tag}
621 }
622  
623 15 "
624 But take your time, think a lot
625 Think of everything you've got
626 For you will still be here tomorrow
627 But your dreams may not
628 "
629  
630 __END__
« parse_grammar ↑ Summary blib/lib/Gedcom/Grammar.pm »