perlgolf hole - Smallest Repeating Pattern

Hole type: subroutine

Write a subroutine that accepts a string which may consist of a repeating pattern, and returns the smallest repeating substring. If the string does not consist of a repeating pattern, the subroutine should return undef or the empty string.

e.g.:

  input       output
  'aaaaaa'    'a'
  'ababab'    'ab'
  'aabaab'    'aab'
  'ababaa'    ''

Solution