You can get away with $keys[$index-1] and
$keys[($index+1) % @keys] because the first
array access, should it be negative, will access from the
back of the array, and the last array access is guaranteed
to be in range. So no extra checking is needed for boundaries.