Wills.pm

filter: Statementstmt 86.3% 82 / 95 Branchbran 63.3% 19 / 30 Conditioncond 45.8% 11 / 24 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 100% 15 / 15 Podpod 100% 2 / 2 totaltotal 72.3% 131 / 181 CC 25 SCAR 26.3
CC
25
Cov
75%
new
21.4
search
18.6
BEGIN
1.0
CRAP 13.9
↑ Summary Wills/wills.pm »
1 package Genealogy::Wills;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Data::Reuse;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Genealogy::Wills::wills;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Module::Info;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Object::Configure 0.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Params::Get 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Params::Validate::Strict 0.09;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Return::Set;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 Genealogy::Wills - Lookup in a database of wills
19  
20 =head1 VERSION
21  
22 Version 0.10
23  
24 =cut
25  
26 5 our $VERSION = '0.10';
27  
28 # Class-level constants
29 use constant {
30 5 DEFAULT_CACHE_DURATION => '1 day', # The database is updated daily
31 MIN_LAST_NAME_LENGTH => 1,
32 MAX_LAST_NAME_LENGTH => 100,
33 5 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 =head1 DESCRIPTION
36  
37 This module provides a convenient interface to search through a database of historical wills,
38 primarily focused on the Kent Wills Transcript.
39 It handles database connections, caching, and result formatting.
40  
41 - Results are cached for 1 day by default
42 - Database connections are lazy-loaded
43 - Large result sets may consume significant memory
44  
45 =head1 SYNOPSIS
46  
47 # See https://freepages.rootsweb.com/~mrawson/genealogy/wills.html
48 use Genealogy::Wills;
49 my $wills = Genealogy::Wills->new();
50 # ...
51  
52 =head1 SUBROUTINES/METHODS
53  
54 =head2 new
55  
56 Creates a Genealogy::Wills object.
57  
58 Takes three optional arguments,
59 which can be hash, hash-ref or key-value pairs.
60  
61 =over 4
62  
63 =item * C<config_file>
64  
65 Points to a configuration file which contains the parameters to C<new()>.
66 The file can be in any common format,
67 including C<YAML>, C<XML>, and C<INI>.
68 This allows the parameters to be set at run time.
69  
70 =item * C<directory>
71  
72 That is the directory containing wills.sql.
73 If not given, the use the module's data directory.
74  
75 =item * C<logger>
76  
77 An object to send log messages to
78  
79 =back
80  
81 =cut
82  
83 sub new
84 {
85 14 ▶ my $class = shift;
Subroutine new: called CC=16 SCAR=30.7
Pod documented
86 14 my $params;
87  
88 # Handle hash or hashref arguments
89 14 ▶ if((scalar(@_) == 1) && !ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and not ref $_[0]) { } 0 14 14
Condition33%1/3
scalar @_ == 1 and not ref $_[0]
!ll&&!rl&&r
1400
MC/DC0%0/2
scalar @_ == 1 and not ref $_[0]
scalar @_ == 1 not ref $_[0]
Truth table33%1/3
A and not(B)
A= scalar @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
90 0 $params->{'directory'} = $_[0];
91 } else {
92 14 $params = Params::Get::get_params(undef, \@_);
93 }
94  
95 14 ▶ if(!defined($class)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $class) { } 1 13 14
elsif (builtin::blessed($class)) { } 2 11 13
96 1 ▶ if((scalar keys %{$params}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$params;} > 0) 0 1 1
97 # Using Genealogy::Wills::new(), not Genealogy::Wills->new()
98 0 carp(__PACKAGE__, ' use ->new() not ::new() to instantiate');
99 0 return;
100 }
101  
102 # FIXME: this only works when no arguments are given
103 1 $class = __PACKAGE__;
104 } elsif(Scalar::Util::blessed($class)) {
105 # clone the given object
106 2 ▶ if($params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params) 1 1 2
107 1 return bless { %{$class}, %{$params} }, ref($class);
108 }
109 1 return bless $class, ref($class);
110 }
111  
112 # Load the configuration from a config file, if provided
113 12 $params = Object::Configure::configure($class, $params);
114  
115 12 ▶ if(!defined(my $directory = ($params->{'directory'} || $Genealogy::Wills::wills->{'directory'}))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined(my $directory = $$params{"directory"} || $$Genealogy::Wills::wills{"directory"})) 6 6 12
Condition66%2/3
$$params{'directory'} || $$Genealogy::Wills::wills{'directory'}
l!l&&r!l&&!r
606
MC/DC50%1/2
$$params{'directory'} || $$Genealogy::Wills::wills{'directory'}
$$params{'directory'} $$Genealogy::Wills::wills{'directory'}
Truth table66%2/3
A || B
A= $$params{'directory'}B= $$Genealogy::Wills::wills{'directory'}
ABresult
1X1
011
000
116 # If the directory argument isn't given, see if we can find the data
117 6 ▶ $directory ||= Module::Info->new_from_loaded(__PACKAGE__)->file();
Condition33%1/3
$directory ||= "Module::Info"->new_from_loaded(__PACKAGE__)->file
l!l&&r!l&&!r
060
MC/DC0%0/2
$directory ||= "Module::Info"->new_from_loaded(__PACKAGE__)->file
$directory "Module::Info"->new_from_loaded(__PACKAGE__)->file
Truth table33%1/3
A ||= B
A= $directoryB= "Module::Info"->new_from_loaded(__PACKAGE__)->file
ABresult
1X1
011
000
118 6 $directory =~ s/\.pm$//;
119 6 $params->{'directory'} = File::Spec->catfile($directory, 'data');
120 }
121  
122 12 ▶ unless((-d $params->{'directory'}) && (-r $params->{'directory'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $$params{"directory"} and -r $$params{"directory"}) 2 10 12
Condition66%2/3
-d $$params{"directory"} and -r $$params{"directory"}
!ll&&!rl&&r
2010
MC/DC50%1/2
-d $$params{"directory"} and -r $$params{"directory"}
-d $$params{"directory"} -r $$params{"directory"}
Truth table66%2/3
A and B
A= -d $$params{"directory"}B= -r $$params{"directory"}
ABresult
0X0
100
111
123 2 Carp::carp(__PACKAGE__, ': ', $params->{'directory'}, ' is not a directory');
124 2 return;
125 }
126  
127 # Validate logger object has required methods
128 10 ▶ if(defined $params->{'logger'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$params{"logger"}) 10 0 10
129 10 ▶ unless(Scalar::Util::blessed($params->{'logger'}) && $params->{'logger'}->can('info') && $params->{'logger'}->can('error')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($$params{"logger"}) and $$params{"logger"}->can("info") and $$params{"logger"}->can("error")) 0 10 10
Condition33%2/6
builtin::blessed($$params{"logger"}) and $$params{"logger"}->can("info") and $$params{"logger"}->can("error")
!ll&&!rl&&r
0010
builtin::blessed($$params{"logger"}) and $$params{"logger"}->can("info")
!ll&&!rl&&r
0010
MC/DC0%0/3
builtin::blessed($$params{"logger"}) and $$params{"logger"}->can("info") and $$params{"logger"}->can("error")
builtin::blessed($$params{"logger"}) $$params{"logger"}->can("info") $$params{"logger"}->can("error")
Truth table25%1/4
A and B and C
A= builtin::blessed($$params{"logger"})B= $$params{"logger"}->can("info")C= $$params{"logger"}->can("error")
ABCresult
0XX0
10X0
1100
1111
130 0 Carp::croak("Logger must be an object with info() and error() methods");
131 }
132 }
133  
134 # cache_duration can be overriden by the args
135 return bless {
136 cache_duration => DEFAULT_CACHE_DURATION,
137 10 %{$params}
138 }, $class;
139 }
140  
141 =head2 search
142  
143 Last (last name) is a mandatory parameter.
144  
145 Return a list of hash references in list context,
146 or a hash reference in scalar context.
147  
148 Each record includes a formatted C<url> field.
149  
150 my $wills = Genealogy::Wills->new();
151  
152 # Returns an array of hashrefs
153 my @smiths = $wills->search(last => 'Smith'); # You must at least define the last name to search for
154  
155 print $smiths[0]->{'first'}, "\n";
156  
157 =cut
158  
159 sub search {
160 4 ▶ my $self = shift;
Subroutine search: called CC=10 SCAR=29.2
Pod documented
161  
162 # Ensure $self is valid
163 4 ▶ Carp::croak('search() must be called on an object') unless(Scalar::Util::blessed($self));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 0 4 4
164  
165 4 my $params = Params::Validate::Strict::validate_strict({
166 args => Params::Get::get_params('last', @_),
167 schema => {
168 'last' => {
169 type => 'string',
170 min => 1,
171 max => 100,
172 matches => qr/^[\w\-]+$/ # Allow hyphens in surnames
173 }, 'first' => {
174 type => 'string',
175 optional => 1,
176 min => 1,
177 max => 100
178 }, 'middle' => {
179 type => 'string',
180 optional => 1,
181 min => 1,
182 max => 100
183 }, 'town' => {
184 type => 'string',
185 optional => 1,
186 min => 1,
187 max => 100
188 }, 'year' => {
189 type => 'integer',
190 optional => 1,
191 min => 1,
192 max => 2025
193 }
194 }
195 });
196  
197 # Validate required parameters thoroughly
198 3 ▶ unless((defined($params->{'last'})) && (length($params->{'last'}) > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$params{"last"} and length $$params{"last"} > 0) 0 3 3
Condition33%1/3
defined $$params{"last"} and length $$params{"last"} > 0
!ll&&!rl&&r
003
MC/DC0%0/2
defined $$params{"last"} and length $$params{"last"} > 0
defined $$params{"last"} length $$params{"last"} > 0
Truth table33%1/3
A and B
A= defined $$params{"last"}B= length $$params{"last"} > 0
ABresult
0X0
100
111
199 0 Carp::carp("Value for 'last' is mandatory");
200 0 return;
201 }
202  
203 # Sanitize input to prevent SQL injection
204 3 $params->{'last'} =~ s/[^\w\s\-']//g; # Allow only word chars, spaces, hyphens, apostrophes
205  
206 3 ▶ $self->{'wills'} ||= Genealogy::Wills::wills->new(no_entry => 1, no_fixate => 1, %{$self});
Condition66%2/3
$$self{"wills"} ||= "Genealogy::Wills::wills"->new("no_entry", 1, "no_fixate", 1, %{$self;})
l!l&&r!l&&!r
210
MC/DC0%0/2
$$self{"wills"} ||= "Genealogy::Wills::wills"->new("no_entry", 1, "no_fixate", 1, %{$self;})
$$self{"wills"} "Genealogy::Wills::wills"->new("no_entry", 1, "no_fixate", 1, %{$self;})
Truth table66%2/3
A ||= B
A= $$self{"wills"}B= "Genealogy::Wills::wills"->new("no_entry", 1, "no_fixate", 1, %{$self;})
ABresult
1X1
011
000
207  
208 3 ▶ if(!defined($self->{'wills'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$self{"wills"}) 0 3 3
209 0 Carp::croak("Can't open the wills database");
210 }
211  
212 3 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 3 0 3
213 3 ▶ if(my $willslist = $self->{'wills'}->selectall_hashref($params)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $willslist = $$self{"wills"}->selectall_hashref($params)) 3 0 3
214 3 my @wills = @{$willslist};
215 3 foreach my $will(@wills) {
216 3 $will->{'url'} = 'https://' . $will->{'url'};
217 }
218 3 Data::Reuse::fixate(@wills);
219 3 return @wills;
220 }
221 0 return;
222 }
223 0 ▶ if(defined(my $will = $self->{'wills'}->fetchrow_hashref($params))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $will = $$self{"wills"}->fetchrow_hashref($params))) 0 0 0
224 0 $will->{'url'} = 'https://' . $will->{'url'};
225 0 Data::Reuse::fixate(%{$will});
226  
227 0 return Return::Set::set_return($will, { 'type' => 'hashref', 'min' => 1 });
228 }
229 }
230  
231 =encoding utf-8
232  
233 =head1 FORMAL SPECIFICATION
234  
235 [NAME, URL, DIRECTORY]
236  
237 WillRecord == [
238 first: NAME;
239 last: NAME;
240 url: URL;
241 additional_fields: ℙ(NAME × seq CHAR)
242 ]
243  
244 WillsDatabase == [
245 directory: DIRECTORY;
246 cache_duration: ℕ;
247 logger: LOGGER
248 ]
249  
250 SearchParams == [
251 last: NAME;
252 first: NAME;
253 optional_params: ℙ(NAME × seq CHAR)
254 ]
255  
256 │ last ≠ ∅ -- last name cannot be empty
257 │ |last| > 0 -- last name must have positive length
258  
259 search: WillsDatabase × SearchParams → ℙ WillRecord
260  
261 ∀ db: WillsDatabase; params: SearchParams •
262 params.last ≠ ∅ ⇒
263 search(db, params) = {r: WillRecord | r.last = params.last ∧ matches(r, params)}
264  
265 ∀ db: WillsDatabase; params: SearchParams •
266 params.last = ∅ ⇒
267 search(db, params) = ∅
268  
269 =head1 AUTHOR
270  
271 Nigel Horne, C<< <njh at nigelhorne.com> >>
272  
273 =head1 BUGS
274  
275 =head1 SEE ALSO
276  
277 The Kent Wills Transcript, L<https://freepages.rootsweb.com/~mrawson/genealogy/wills.html>
278  
279 =head1 SUPPORT
280  
281 You can find documentation for this module with the perldoc command.
282  
283 perldoc Genealogy::Wills
284  
285 You can also look for information at:
286  
287 =over 4
288  
289 =item * MetaCPAN
290  
291 L<https://metacpan.org/release/Genealogy-Wills>
292  
293 =item * RT: CPAN's request tracker
294  
295 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Genealogy-Wills>
296  
297 =item * CPAN Testers' Matrix
298  
299 L<http://matrix.cpantesters.org/?dist=Genealogy-Wills>
300  
301 =item * CPAN Testers Dependencies
302  
303 L<http://deps.cpantesters.org/?module=Genealogy::Wills>
304  
305 =back
306  
307 =head1 LICENSE AND COPYRIGHT
308  
309 Copyright 2023-2025 Nigel Horne.
310  
311 This program is released under the following licence: GPL2
312  
313 =cut
314  
315 5 1;
↑ Summary Wills/wills.pm »