#!/usr/bin/perl

open(DESC, "descendants") || die "without issue\n";

# Load the kids of Job.

&load_kids('');

sub load_kids {
    local($parent) = @_;
    local($name);

    # Process all the current parent's children.

    while (<DESC>) {
	last if /}/;

	# Extract name from line with a regular expression.

	next unless /(\w.*\w)/;
	$name = $1;

	# Use associative array to store a tree.

	$parent{$name} = $parent;

	# See if this kid has kids.

	if (/{/) {
	    &load_kids($name);
	}
    }
}

# Now we ask which name to print the lineage of, and print it.

while (1) {
    print "Who: ";
    chop($who = <STDIN>);
    last unless $who;
    &do_a_begat($who);
}

# Recursively follow the tree of parents up to Job, printing
# "begat" lines on the way back down.  (We're really just
# showing off, since we could just as easily have done this
# linear traverse of the tree using an ordinary loop.)

sub do_a_begat {
    local($name) = @_;

    if ($parent{$name}) {
	&do_a_begat($parent{$name});
	print "$parent{$name} begat $name\n";
   }
}
