The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

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 can be a string or 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 limitation 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.

Constructor

The constructor for this class takes one optional attribute called `abbr`. The default value for this attribute is 2. When set, strings of repeated "great"s in a relationship description will collapsed to "$n x great".

For example, if the description you have is "Great, great, great grandfather", then that will be abbreviated to to "3 x great grandfather".

The value for `abbr` is the maximum number of repetitions that will be left untouched. You can turn abbreviations off by setting `abbr` to zero.

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.

abbr_rel

Optionally abbreviate a relationship description.

make_rel

Given relationship co-ords and a gender, this will synthesise a relationship description. This only works because we've hard-coded an initial relationship table that covers all of the trickier situations.

times_str

Given an integer, this method returns a string version for use in a "removed" cousin relationship, i.e. "once", "twice", "three times", etc.

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.

get_relationship_ancestors

Given two people, returns lists of people linking those two people to their most recent common ancestor.

The return value is a reference to an array containing two array references. The first references array contains the person1 and all their ancestors up to an including the most recent common ancestor. The second list does the same for person2.

AUTHOR

Dave Cross <dave@perlhacks.com>

SEE ALSO

perl(1)

COPYRIGHT AND LICENSE

Copyright (C) 2018-2023, 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.