in reply to
Calculate the overlap length between two ranges
Two ranges A and B overlap if low(A) <= high(B) AND high(A) >= low(B) ... hope I got that right, but it’s something like that. Yes, there should be a CPAN module as-noted which does this simple task and probably others that will be of interest to you.
Then, again IIRC, the distance ought to be something like Max(Low(A), High(B)) - Min(Low(B), High(A)) ... not so sure about that one, it’s been a while. Anyway, I do remember that you are testing the opposite-end numbers in each pair in some way.