NAME
Genealogy::Relationship - calculate the relationship between two people
SYNOPSIS
use Genealogy::Relationship;
use Person; # Imaginary class modelling people
my $rel = Genealogy::Relationship->new;
my $grandfather = Person->new( ... );
my $father = Person->new( ... );
my $me = Person->new( ... );
my $aunt = Person->new( ... );
my $cousin = Person->new( ... );
my $common_ancestor = $rel->get_most_recent_common_ancestor(
$me, $cousin,
);
say $common_ancestor->name; # Grandfather's name
say $rel->get_relationship($me, $grandfather); # Grandson
say $rel->get_relationship($grandfather, $me); # Grandfather
say $rel->get_relationship($father, $cousin); # Uncle
say $rel->get_relationship($cousin, $father); # Niece
DESCRIPTION
This module makes it easy to calculate the relationship between two people.
If you have a set of objects modelling your family tree, then you will be able to use this module to get a description of the relationship between any two people on that tree.
The objects that you use with this module need to implement three methods:
parent
This method should return the object which is the parent of the current person.
id
This method should return a unique identifier for the current person. The identifier should be a number.
gender
This method should return the gender of the current person. It should be the character 'm' or 'f'.
Limitations
This module was born out of a need I had while creating https://lineofsuccession.co.uk/. This leads to a couple of limitations that I hope to remove at a later date.
Each person in the tree is expected to have only one parent. This is, of course, about half of the usual number. It's like that because for the line of succession I'm tracing bloodlines and only one parent is ever going to be significant.
I realise that this is a significant limitation and I'll be thinking about how to fix it as soon as possible.
The table that I use to generate the relationship names only goes back five generations - that's to fourth cousins (people who share great, great, great grandparents with each other).
This has, so far, been enough for my purposes, but I realise that more coverage would be useful. I should probably move away from a table-based approach and find a way to calculate the relationship names.
Caching
Calculating relationship names isn't at all different. But there can be a lot of (simple and repetitive) work involved. This is particularly true if your objects are based on database tables (as I found to my expense).
If you're calculating a lot of relationships, then you should probably consider putting a caching layer in front of get_relationship
.
Methods
The following methods are defined.
most_recent_common_ancestor
Given two person objects, returns the person who is the most recent common ancestor for the given people.
get_ancestors
Given a person object, returns a list of person objects, one for each ancestor of the given person.
The first person in the list will be the person's parent and the last person will be their most distant ancestor.
get_relationship
Given two person objects, returns a string containing a description of the relationship between those two people.
get_relationship_coords
Given two person objects, returns the "co-ordinates" of the relationship between them.
The relationship co-ordinates are a pair of integers. The first integer is the number of generations between the first person and their most recent common ancestor. The second integer is the number of generations between the second person and their most recent common ancestor.
AUTHOR
Dave Cross <dave@perlhacks.com>
SEE ALSO
perl(1)
COPYRIGHT AND LICENSE
Copyright (C) 2018-2020, Magnum Solutions Ltd. All Rights Reserved.
This script is free software; you can redistribute it and/or modify it under the same terms as Perl itself.